gpt4 book ai didi

java - Selenium WebDriver 无法识别元素,尝试了很多选项

转载 作者:太空宇宙 更新时间:2023-11-04 14:14:53 24 4
gpt4 key购买 nike

我是 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>

`

最佳答案

这段代码中似乎没有 iframe,但我认为你应该尝试

driver.findElement(By.cssSelector("button#submit_approve_access")).click();

这将找到 ID 为“submit_approve_access”的按钮。如果您正在寻找输入元素,请尝试

driver.findElement(By.cssSelector("input#submit_access")).click();

如果这不起作用,我能想到的最后一件事就是这个

(driver.findElement(By.cssSelector("input#submit_access"))).click();

这些额外的括号可确保在对象上执行 .click。它不应该有什么不同,但也有可能。

此外,关于灰色元素,您不应使用隐式等待代码。这是当你使用

时的情况
driver.get("");

尝试:

try{
Thread.sleep(500);
}catch(Exception e){//Use this before you find the element
e.printStackTrace();
}

而不是:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

关于java - Selenium WebDriver 无法识别元素,尝试了很多选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27871114/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com