2015-01-09 84 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();