2015-01-09 107 views
1

我是新的硒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> 

`

+2

您是否尝试过使用WebDriverWait进行显式等待? – alecxe

+0

感谢@alecxe,使用WebDriverWait我得到一个超时。硒只是看不到按钮。尽管所有的帮助,我仍然感到困惑,但我已经了解了更多有关硒的知识。 – 3Dee

回答

0

似乎没有IFRAME s,但我认为你应该尝试

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

这会找到ID为“submit_approve_access”的按钮。 如果您正在寻找input元素然后尝试

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

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

(driver.findElement(By.cssSelector("input#submit_access"))).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); 
+0

@ColdConfusion,谢谢你的建议。我已经尝试了以上所有内容,但不幸的是,它们也不起作用。 – 3Dee

+0

@ColdConfusion,更具体地说,我得到“NoSuchElementException”,然后当我使用睡眠时,我得到一个超时。我现在认为这个页面有些奇怪或者与其他页面有所不同,因为这个页面是通过使用我们产品中的Google API引发的。 – 3Dee

+0

这很奇怪。哦,这就是我所知道的。抱歉。 –

0

我会尝试使用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(); 
+0

感谢@Saifur的建议。使用webdriverwait时,出现java.lang.AssertionError:timeout。 Selenium Webdriver只是看不到按钮。 – 3Dee

+0

然后点击将不起作用,因为除非您执行'javascript',否则'selenium'不会与隐藏元素交互。你能提供完整的堆栈跟踪吗? – Saifur

+0

以下是完整的堆栈跟踪(我必须将其拆分,因为它太大而不像单个注释): 'java.lang.AssertionError:timeout \t at org.junit.Assert.fail(Assert.java:88) \t在ConnectAppsOAuthTest.connectAppsJunitTest(ConnectAppsOAuthTest.java:100) \t在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法) \t在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) \t在sun.reflect .DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) \t at java.lang.reflect.Method.invoke(Method.java:597)' – 3Dee

0

务必点击你应该等待元素可以存在任何元素之前使用以下一段代码:

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