我試圖在 VBA 中使用 selenium 點擊一個按鈕,我設定了這條線
Set ele = .FindElementByXPath("//div/button[@ng-click='VerifyData()']")
在網頁上,我可以找到這個 XPath 并且它是正確的,但是在代碼中嘗試它時,出現錯誤 No such Element 我在 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> <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&k=6LdpU8gZAAAAAO6-5k51vSLoNVV1Wmsc77S7ptlV&co=aHR0cHM6Ly9vZmZpY2UzNjUuZW1pcy5nb3YuZWc6NDQz&hl=ar&v=rPvs0Nyx3sANE-ZHUN-0nM85&size=normal&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
<div id="recaptcha02" 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&k=6LdpU8gZAAAAAO6-5k51vSLoNVV1Wmsc77S7ptlV&co=aHR0cHM6Ly9vZmZpY2UzNjUuZW1pcy5nb3YuZWc6NDQz&hl=ar&v=rPvs0Nyx3sANE-ZHUN-0nM85&size=normal&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"></iframe></div><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;"></textarea></div></div>
我無法檢查那個方塊,雖然我可以在前一頁做
uj5u.com熱心網友回復:
請檢查dev tools(谷歌瀏覽器)我們是否有唯一的條目HTML DOM。
檢查步驟:
Press F12 in Chrome- >去element節- >做一個CTRL F- >再貼上xpath看看,如果你需要的element是越來越強調與1/1匹配的節點。
如果這是唯一的,//div/button[@ng-click='VerifyData()']那么您還需要檢查以下條件。
- 檢查它是否在任何
iframe/frame/frameset. - 檢查它是否在任何
shadow-root. - 確保在與元素互動之前正確呈現元素。放一些
hardcoded delay或Explicit wait再試一次。 - 如果您已重定向到 a
new tab/ or new windows并且您還沒有切換到該特定的new tab/new window,否則您可能會遇到NoSuchElement例外。 - 如果您已切換到 iframe 并且新的所需元素不在同一個 iframe 背景關系中,則首先
switch to default content與它互動。
切換到defaultcontent:
driver.SwitchToDefaultContent
uj5u.com熱心網友回復:
由于您沒有分享該頁面的鏈接,我只能猜測。
所以,而不是
//div/button[@ng-click='VerifyData()']
你可以嘗試使用
//button[contains(@ng-click,'VerifyData')]
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/380747.html
