Home > Enterprise >  Click button raises error no such element
Click button raises error no such element

Time:12-13

I am trying to click on a button using selenium in VBA and I set this line

Set ele = .FindElementByXPath("//div/button[@ng-click='VerifyData()']")

On the webpage, I can find this XPath and it is correct but when trying it in the code, I got an error No Such Element I have searched for iframe in the HTML page but no clue

<div >
                <div >
                    <div >
                        <a style="color: #ea3c00; font-size: 20px;"  ng-click="BackToHome()"><i ></i> </a>
                        <h5>
                             
                        </h5>
                    </div>
                    <div >
                        <label for=""><span> أدخل   </span>&nbsp; <span style="font-size:.7em;" ng-show="EmailType == 1" ></span></label>
                        <input type="text" pattern="\d*"  maxlength="16" ng-model="NationalId">
                    </div>
                    <div >
                        <label for="" >ss</label>
                        <input type="text" pattern="\d*"  maxlength="16" ng-model="PrincipleCode">
                    </div>
                    <div >
                        <div id="recaptcha01"  data-sitekey="6LdpU8gZAAAAAO6-5k51vSLoNVV1Wmsc77S7ptlV"><div style="width: 304px; height: 78px;"><div><iframe title="reCAPTCHA" src="https://www.google.com/recaptcha/api2/anchor?ar=1&amp;k=6LdpU8gZAAAAAO6-5k51vSLoNVV1Wmsc77S7ptlV&amp;co=aHR0cHM6Ly9vZmZpY2UzNjUuZW1pcy5nb3YuZWc6NDQz&amp;hl=ar&amp;v=rPvs0Nyx3sANE-ZHUN-0nM85&amp;size=normal&amp;cb=k31qvndf1wp5" width="304" height="78" role="presentation" name="a-g716ep6e9b8d" frameborder="0" scrolling="no" sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-top-navigation allow-modals allow-popups-to-escape-sandbox"></iframe></div><textarea id="g-recaptcha-response" name="g-recaptcha-response"  style="width: 250px; height: 40px; border: 1px solid rgb(193, 193, 193); margin: 10px 25px; padding: 0px; resize: none; display: none;"></textarea></div></div>
                    </div>
                    <div >
                        <button  ng-click="VerifyData()" ng-show="Loading == false">CHECK </button>
                        <img src="/wwwroot/images/loader.gif" width="60" ng-show="Loading == true" >
                    </div>
                    <div  ng-show="ErrorMessage !=''">
                        
                    </div>
                </div>
           
        </div>

This is the code I am trying till now

.AddArgument "--disable-notifications"
.Start "Chrome"
.Get "https://office365.emis.gov.eg/"
.Wait 1000
.FindElementByXPath("//button[@ng-click='SetEmailType(1)']").Click
With .FindElementByXPath("//input[@ng-model='NationalId']")
    .Clear: .SendKeys shEF.Cells(ActiveCell.Row, 2).Value
End With
With .FindElementByXPath("//input[@ng-model='PrincipleCode']")
    .Clear: .SendKeys shEF.Cells(ActiveCell.Row, 1).Value
End With
.SwitchToFrame .FindElementByTag("iframe", timeout:=10000)
.Wait 1000
.FindElementByXPath("//*[@id='recaptcha-anchor']").Click
.Wait 1000

CodePudding user response:

Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

If this is unique //div/button[@ng-click='VerifyData()'] then you need to check for the below conditions as well.

  1. Check if it's in any iframe/frame/frameset.
  2. Check if it's in any shadow-root.
  3. Make sure that the element is rendered properly before interacting with it. Put some hardcoded delay or Explicit wait and try again.
  4. If you have redirected to a new tab/ or new windows and you have not switched to that particular new tab/new window, otherwise you will likely get NoSuchElement exception.
  5. If you have switched to an iframe and the new desired element is not in the same iframe context then first switch to default content and then interact with it.

Switch to defaultcontent:

driver.SwitchToDefaultContent

CodePudding user response:

Since you didn't share a link to that page I can only guess.
So, instead of

//div/button[@ng-click='VerifyData()']

you can try using

//button[contains(@ng-click,'VerifyData')]
  • Related