【问题标题】:Selenium WebDriver not recognizing an element, tried many optionsSelenium WebDriver 无法识别元素,尝试了很多选项
【发布时间】: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


【解决方案1】:

此代码中似乎没有 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);

【讨论】:

  • @ColdConfusion,感谢您的建议。我已经尝试了以上所有方法,但不幸的是它们也不起作用。
  • @ColdConfusion,更具体地说,我得到“NoSuchElementException”,然后当我使用睡眠时,我得到了超时。我现在认为这个页面有些奇怪或与其他页面不同,因为这个页面是通过使用我们产品中的 Google API 唤起的。
  • 嗯,这很奇怪。哦,好吧,这就是我所知道的。对不起。
  • 还是谢谢你,我从你那里学到了很多,为以后的 Selenium 测试做准备。 :-)
【解决方案2】:

我会尝试使用explicit 等等。如果您使用的是ie,则另一条信息有一个已知问题,如果您已更新 [KB 3025390] (https://code.google.com/p/selenium/issues/detail?id=8302)

WebElement myDynamicElement = (new WebDriverWait(driver, 10))
  .until(ExpectedConditions.presenceOfElementLocated(By.id("submit_approve_access")));
driver.findElement(By.id("submit_approve_access")).click();

【讨论】:

  • 感谢@Saifur 的建议。使用 webdriverwait 时出现 java.lang.AssertionError:timeout。 Selenium Webdriver 只是看不到按钮。
  • 那么点击也不起作用,因为selenium不会与隐藏元素交互,除非你执行javascript。你能提供完整的堆栈跟踪吗?
  • 下面是完整的堆栈跟踪(我必须拆分它,因为它作为单个评论太大了):java.lang.AssertionError: timeout at org.junit.Assert.fail(Assert.java:88) at ConnectAppsOAuthTest.connectAppsJunitTest(ConnectAppsOAuthTest.java:100) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597)
  • ' 在 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44) 在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 在 org.junit.internal.runners.statements .RunBefores.evaluate(RunBefores.java:26) at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) `
  • ` 在 org.junit.runners 的 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) 的 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 在 org.junit.runners。 ParentRunner$3.run(ParentRunner.java:238) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236) at org.junit .runners.ParentRunner.access$000(ParentRunner.java:53) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229) at org.junit.runners.ParentRunner.run(ParentRunner.java:309) `
【解决方案3】:

始终在单击任何元素之前,您应该等待该元素的存在,您可以使用以下代码:

WebDriverWait wait = new WebDriverWait(driver, Flow.timeOutLimit);
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.id("submit_approve_access")));          
driver.findElement(By.id(submit_approve_access)).click(); 

【讨论】:

    猜你喜欢
    • 2016-01-16
    • 2016-04-05
    • 1970-01-01
    • 2013-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    • 2016-04-14
    相关资源
    最近更新 更多