【发布时间】:2015-01-09 23:45:06
【问题描述】:
我是 selenium web 驱动程序的新手,但到目前为止已经成功编写了几个使用它的 Junit 测试。我现在正在进行第三次测试,遇到了无法找到元素的问题。我从 Selenium 收到错误“NoSuchElementException”。我花了几个小时尝试了许多选择(见下文)。
简而言之,我正在测试的产品是第三方产品,确切地说是与 Google Cloud Storage 连接。给出问题的页面实际上是由 Google 编写的页面,因此我无法与开发人员交谈以查看是否使用了框架,也无法从 html 中分辨出来,但是有一个名为“framebuster 代码”的灰色部分,所以可能有帧? (见下文)。
我确实尝试通过使用“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,使用 WebDriverWait 我遇到了超时。 Selenium 只是看不到按钮。我仍然感到困惑,尽管有所有的帮助,但我对 Selenium 有了更多的了解。
标签: java selenium iframe selenium-webdriver