2015-12-21 50 views
0

我用下面的代碼來檢索,我想在硒選擇行:找到一個TR標籤的複選框,並在硒(JAVA)選擇它

WebElement row = driver.findElement(By.xpath("//tr[contains(.,'prod1')]")); 

其結果是,我得到以下列:

<th width="18px"> 
       <input class="selectedProducts" id="checkbox-37640" onclick="markedProductsHandler.toggleMarkProduct(this,37640)" type="checkbox"> 
      </th> 


      <th style="white-space:nowrap; width:573px;" align="left"> 

<a class="maxTextSize" style="width:530px" href="product.html?id=37640&amp;rm7zz5c=ZBOeBCOAs">aa prod1</a> 
      </th> 

      <th style="white-space:nowrap;" align="right" width="140px"> 

        <a href="product-edit.html?id=37640&amp;rm7zz5c=ZBOeBCOAs"><img src="_images/icons/pencil.png?rm7zz5c=ZBOeBCOAs" title="Edit"></a> &nbsp; 


         <input name="favoriteid" class="favoriteAction" value="37640" type="hidden"> 
         <input name="favoriteaction" class="favoriteAction" value="add" type="hidden"> 
         <a href="javascript:" onclick="onFavoriteAction(this)"> 
          <img src="_images/icons/star_grey.png?rm7zz5c=ZBOeBCOAs" alt="Watch product" title="Watch product"> 
         </a> &nbsp; 

     <form action="product-pdf.html?rm7zz5c=ZBOeBCOAs" method="post"> 
      <input name="export" value="pdf" type="hidden"> 
      <input name="id" value="37640" type="hidden"> 
      <a href="javascript:" onclick="javascript: $(this).parent().submit();"><img src="_images/icons/page_white_acrobat.png?rm7zz5c=ZBOeBCOAs" title="Download as PDF"></a> &nbsp; 
     </form> 
      </th> 

現在我想請選中該複選框:

row.findElement(By.xpath("//input[@type='checkbox']")).click(); 

,但我得到了以下錯誤:

org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with 

有人能幫助我嗎?謝謝。

+2

根據例外情況,它好像隱藏了複選框。對於'selectedProducts'類,有沒有像'visibility:hidden;'或'display:none'這樣的風格?https://github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/openqa/selenium /ElementNotVisibleException.java – osigge

+1

你能告訴我們'row.findElement(By.xpath(「//輸入[@ type ='checkbox']」))。isDisplayed()'?的結果嗎? +嘗試添加服務員等待,直到複選框將變得可見 – drets

+0

嗨,結果是假的 –

回答

1

嘗試添加明確的等待直到元素是可見的。如果元件永久性地隱藏(?因爲某些原因)嘗試以下操作:

JavascriptExecutor js = (JavascriptExecutor)driver; 
WebElement element = row.findElement(By.xpath("//input[@type='checkbox']")); 
js.executeScript("arguments[0].click();", element); 

之一,如果一切會失敗的想法:markedProductsHandler.toggleMarkProduct

+0

交互謝謝,但它不起作用。 –

+1

:(最後一次機會:你能分享到頁面的鏈接嗎? – drets

+0

謝謝,但這是不可能的,它怎麼可以幫助? –

0

我覺得複選框的ID是動態的,所以你應該像下面生成的XPath:

row.findElement(By.xpath("//input[@id[starts-with(., 'checkbox')]]")).click(); 
+0

謝謝,但我仍然得到錯誤org.openqa.selenium.ElementNotVisibleException:元素目前不可見,因此可能不會與 –

1

嘗試fluentWait:您可以通過JavaScript火下面的方法?

WebDriverWait wait = new WebDriverWait(driver, timeout); 
wait.until(ExpectedConditions.visibilityOf 
       (driver.findElement(By.xpath("//input[@type='checkbox']")))); 

檢查是否會拋出異常。

+0

嗨,這是結果org.openqa.selenium.TimeoutException:等待[[FirefoxDriver:WINDOWS上的firefox]]的可見性5秒後超時 - > xpath: //輸入[@類型= '複選框']] –

相關問題