我是新的硒web驅動程序,但已成功地使用它到目前爲止已經寫了幾個Junit測試。我現在正在進行第三次測試,並遇到無法找到元素的問題。我收到來自Selenium的錯誤「NoSuchElementException
」。我花了幾個小時嘗試很多選擇(見下文)。硒WebDriver不識別元素,嘗試了很多選擇
簡而言之,我測試的產品是第三方產品,確切地說,它與Google雲端存儲相連接。提供問題的頁面實際上是Google編寫的一個頁面,所以我無法與開發人員交談,以查看是否使用了框架,並且我無法從HTML中知道,但是存在一個稱爲「框架代碼」的灰色部分,因此可能存在幀? (見下文)。
我曾嘗試指定使用「driver.switchTo().frame(0);
」
有一個iframe
但也不能工作。
最後,我試圖找到的元素在加載頁面時會立即變灰。我試過了一個隱含的等待, 「driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
」
但這並沒有幫助。
任何關於我可能會做錯的建議或更多建議嘗試將不勝感激。我不能讓這打敗我。 :-)
這裏是我嘗試過的所有選項。注意第一個選項來自IDE,並且它在IDE中正常工作,而不是通過WebDriver。
`driver.findElement(By.id("submit_approve_access")).click();
driver.findElement(By.xpath("(//a[contains(text(),'Accept')])")).click();
driver.findElement(By.name("submit_access")).click();
driver.findElement(By.className("goog-buttonset-action")).click();
driver.findElement(By.cssSelector("input[name=submit_name]")).click();
driver.findElement(By.cssSelector("a[class='goog-buttonset-action']")).click();
driver.findElement(By.linkText(「Accept」)).click();
driver.findElement(By.xpath("//a[@class='goog-buttonset-action']")).click();
driver.findElement(By.xpath("//a[text() = ‘Accept]」)).click();
driver.findElement(By.cssSelector("button[type='submit']")).click();
driver.findElement(By.cssSelector("button[tabindex='1']")).click();`
下面是從頁面的HTML(注意:我要找的就行*表示的元素我也嘗試了一些從它上面的線,以及隱藏的項目):
`
<head></head>
<body>
<noscript></noscript>
<!--
framebuster code starts here
-->
<style></style>
<script></script>
<xmp style="display:none"></xmp>
<!--
framebuster code ends here
-->
<div id="ogb"></div>
<div id="third_party_info_container">
<div id="third_party_info" class="section_container" data-section="main">
<div class="column"></div>
<div id="approval_container">
<div class="column">
<div id="connect_container" class="modal-dialog-buttons button_container">
<form id="connect-approve" style="display: inline;" method="POST" action="https://accounts.google.com/o/oauth2/approval?as=32b8da86447…d=none&xsrfsign=APsBz4gAAAAAVLBpqOjvhQOwuPvNvPdcZF53EntxeTvP">
<input id="bgresponse" type="hidden" name="bgresponse"></input>
<input id="_utf8" type="hidden" value="☃" name="_utf8"></input>
<input id="state_wrapper" type="hidden" value="CoYDcmVzcG9uc2VfdHlwZT1jb2RlJmFjY2Vzc190eXBlPW9mZmxpbmUmcmVk…UucmVhZF9vbmx5EhUxMTY5MjAyNTM4Nzc2NTMxNzA1MTQY7seC5-zsnJbqAQ" name="state_wrapper"></input>
<input id="submit_access" type="hidden" value="" name="submit_access"></input>
*<button id="submit_approve_access" class="goog-buttonset-action" tabindex="1" type="submit"></button>
<button id="submit_deny_access" tabindex="2" type="submit"></button>
</form>
<div class="clear"></div>
</div>
</div>
</div>
</div>
</div>
<div style="display:none"></div>
<div id="tooltip_bubble"></div>
<script type="text/javascript"></script>
<iframe src="https://clients5.google.com/pagead/drt/dn/" aria-hidden="true" style="display: none;"></iframe>
</body>
`
您是否嘗試過使用WebDriverWait進行顯式等待? – alecxe
感謝@alecxe,使用WebDriverWait我得到一個超時。硒只是看不到按鈕。儘管所有的幫助,我仍然感到困惑,但我已經瞭解了更多有關硒的知識。 – 3Dee