【问题标题】:Click button raises error no such element单击按钮引发错误没有这样的元素
【发布时间】:2021-12-12 17:45:15
【问题描述】:

我正在尝试在 VBA 中使用 selenium 单击按钮并设置此行

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

在网页上,我可以找到这个 XPath,它是正确的,但是在代码中尝试它时,我得到一个错误没有这样的元素 我在 HTML 页面中搜索了 iframe,但没有任何线索

<div class="form-details">
                <div class="row">
                    <div class="col-12 text-center">
                        <a style="color: #ea3c00; font-size: 20px;" class="float-right back-button" ng-click="BackToHome()"><i class="fas fa-arrow-right"></i> </a>
                        <h5>
                             
                        </h5>
                    </div>
                    <div class="col-12 form-group form-item">
                        <label for=""><span> أدخل   </span>&nbsp; <span style="font-size:.7em;" ng-show="EmailType == 1" class=""></span></label>
                        <input type="text" pattern="\d*" class="form-control number ng-valid ng-valid-pattern ng-valid-maxlength ng-not-empty ng-dirty ng-valid-parse ng-touched" maxlength="16" ng-model="NationalId">
                    </div>
                    <div class="col-12 form-group form-item">
                        <label for="" class="ng-binding">ss</label>
                        <input type="text" pattern="\d*" class="form-control number ng-valid ng-valid-pattern ng-valid-maxlength ng-not-empty ng-dirty ng-valid-parse ng-touched" maxlength="16" ng-model="PrincipleCode">
                    </div>
                    <div class="col-12 text-center">
                        <div id="recaptcha01" class="g-recaptcha" 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" class="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 class="col-12 mt-3 text-center">
                        <button class="btn check-btn" ng-click="VerifyData()" ng-show="Loading == false">CHECK </button>
                        <img src="/wwwroot/images/loader.gif" width="60" ng-show="Loading == true" class="ng-hide">
                    </div>
                    <div class="col-12 text-center error-container ng-binding ng-hide" ng-show="ErrorMessage !=''">
                        
                    </div>
                </div>
           
        </div>

这是我目前正在尝试的代码

.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

在另一个页面上,还有一个正方形供验证码检查,这里是外层html

&lt;div id="recaptcha02" class="g-recaptcha" data-sitekey="6LdpU8gZAAAAAO6-5k51vSLoNVV1Wmsc77S7ptlV"&gt;&lt;div style="width: 304px; height: 78px;"&gt;&lt;div&gt;&lt;iframe title="reCAPTCHA" src="https://www.google.com/recaptcha/api2/anchor?ar=1&amp;amp;k=6LdpU8gZAAAAAO6-5k51vSLoNVV1Wmsc77S7ptlV&amp;amp;co=aHR0cHM6Ly9vZmZpY2UzNjUuZW1pcy5nb3YuZWc6NDQz&amp;amp;hl=ar&amp;amp;v=rPvs0Nyx3sANE-ZHUN-0nM85&amp;amp;size=normal&amp;amp;cb=yb04gn21huf7" width="304" height="78" role="presentation" name="a-gmezfxiw48et" frameborder="0" scrolling="no" sandbox="allow-forms allow-popups allow-same-origin allow-scripts allow-top-navigation allow-modals allow-popups-to-escape-sandbox"&gt;&lt;/iframe&gt;&lt;/div&gt;&lt;textarea id="g-recaptcha-response-1" name="g-recaptcha-response" class="g-recaptcha-response" style="width: 250px; height: 40px; border: 1px solid rgb(193, 193, 193); margin: 10px 25px; padding: 0px; resize: none; display: none;"&gt;&lt;/textarea&gt;&lt;/div&gt;&lt;/div&gt;

虽然我可以在前一页中检查,但我无法检查那个方格

【问题讨论】:

  • 网站是公开的吗?提到这一点总是有用的。
  • 非常感谢。这是网址https://office365.emis.gov.eg/。点击第二个按钮
  • 当您收到错误消息时,您仍然可以在打开的浏览器中找到该元素吗?
  • driver.ExecuteScript "document.querySelector('[ng-click^=VerifyData]').click();"
  • ExecuteScript 也有效?

标签: vba selenium xpath


【解决方案1】:

如果我们在HTML DOM 中有唯一条目,请检查dev tools(谷歌浏览器)。

检查步骤:

Press F12 in Chrome -> 转到element 部分 -> 做一个CTRL + F -> 然后粘贴xpath 看看,如果你想要的element 用@ 得到高亮 987654328@匹配节点。

如果这是唯一的//div/button[@ng-click='VerifyData()'],那么您还需要检查以下条件。

  1. 检查它是否在任何iframe/frame/frameset 中。
  2. 检查它是否在任何shadow-root 中。
  3. 确保在与元素交互之前正确呈现元素。输入一些hardcoded delayExplicit wait 再试一次。
  4. 如果您已重定向到 new tab/ or new windows 并且您没有切换到特定的 new tab/new window,否则您可能会遇到 NoSuchElement 异常。
  5. 如果您已切换到 iframe 并且所需的新元素不在同一个 iframe 上下文中,则首先 switch to default content 然后与之交互。

切换到defaultcontent

driver.SwitchToDefaultContent

【讨论】:

  • 是的,它是独一无二的,我使用 Ctrl + F 来确定 xpath。
  • 那我提到的 4 个条件呢?我在https://office365.emis.gov.eg/ 收到504 Gateway Time-out 当我在共享HTML 中检查//div/button[@ng-click='VerifyData()'] XPath 时,它存在1/1 所以我们很好,但这不是整个页面HTML。
  • 我没有找到任何 iframe 或 shadow-root。 iframe 在我检查验证码之前就存在,但在那之后它就消失了。
  • 您在代码中切换到 iframe 并且从未切换到 default content 并且由于 //div/button[@ng-click='VerifyData()'] 这不在 iframe 中,您将得到 NoSuchElement exception
  • driver.SwitchToDefaultContent ?
【解决方案2】:

由于您没有分享指向该页面的链接,我只能猜测。
所以,而不是

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

你可以试试

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

【讨论】:

    猜你喜欢
    • 2020-11-22
    • 2021-03-16
    • 2020-01-14
    • 2011-09-16
    • 2017-12-08
    • 2014-10-02
    • 1970-01-01
    • 2017-06-08
    • 1970-01-01
    相关资源
    最近更新 更多