2017-03-17 70 views
0

問題:如何從表列中找到值,並根據該值如何選擇相鄰的複選框。如何處理使用Web驅動程序動態生成值的表?

描述:該表由8欄組成。第一列(列名是'選擇')由複選框組成。在最後一列(列名是'金額') 我們有金額。當相鄰金額欄由數值(比方說)3380組成時,我想讓網絡驅動程序點擊複選框。如果特定金額存在於金額部分,則點擊與該金額行相關的複選框。

enter image description here

備註:這些值本質上是動態的。

HTML代碼:

<table width="760" cellspacing="0" cellpadding="0" background="../images/bground_title_row.gif" height="25"> 
<tr valign="top"><td> 
<table width="760" align="center" cellpadding="0" cellspacing="1"> 
<div align="center"><span class="noninputtextfields">Select</span></div> 
<td height="25" width="50"><div align="center"><span class="noninputtextfields">Select</span></div></td> 
<td height="25" width="50">&nbsp;<span class="noninputtextfields">Date</span></td> 
<td height="25" width="150">&nbsp;<span class="noninputtextfields">Handler</span></td>   
<td height="25" width="100">&nbsp;<span class="noninputtextfields">Payee</span></td> 
<td height="25" width="50" valign="center" nowrap="">&nbsp;<span class="noninputtextfields">Ind</span></td> 
<td height="25" width="210" valign="center">&nbsp;<span class="noninputtextfields">Claim/Claimant</span></td> 
<td height="25" width="100">&nbsp;<span class="noninputtextfields">Insured</span></td> 
<td height="25" width="50"><div align="right"><span class="noninputtextfields">Amount €</span>&nbsp;</div></td> 
</tr> 

<tr> 
<td><input type="checkbox" name="bulk_CHKpayment" value="0" onclick="this.checked=false;></td> 
<td height="25" nowrap="">&nbsp;<span class="inputtextfields">01/03/2017 08:40:41</span></td> 
<td height="25">&nbsp;<span class="inputtextfields">Peter Collins </span></td> 
<td height="25">&nbsp;<span class="inputtextfields">FirstName </span></td> 
<td height="25">&nbsp;<span class="inputtextfields">N </span></td> 
<td height="25">&nbsp;<span class="inputtextfields">FL </span></td> 
<td height="25">&nbsp;<span class="inputtextfields">FirstName</span></td> 
<td><div align="right"><span class="inputtextfields">5045.17&nbsp;</span></div></td> 
</tr> 

<tr> 
<td><input type="checkbox" name="bulk_CHKpayment" value="0" onclick="this.checked=false;></td> 
<td height="25" nowrap="">&nbsp;<span class="inputtextfields">01/03/2017 08:40:41</span></td> 
<td height="25">&nbsp;<span class="inputtextfields">David Dessy </span></td> 
<td height="25">&nbsp;<span class="inputtextfields">FirstName </span></td> 
<td height="25">&nbsp;<span class="inputtextfields">N </span></td> 
<td height="25">&nbsp;<span class="inputtextfields">FL </span></td> 
<td height="25">&nbsp;<span class="inputtextfields">FirstName</span></td> 
<td><div align="right"><span class="inputtextfields">1000&nbsp;</span></div></td> 
<tr> 
. 
. 
. 
</table> 
+0

我不能找到價值100000,任何複選框,你可以分享這些複選框,周圍那幾個(2-5)行的代碼,並刪除不必要的代碼? –

+0

請訪問:http://tinypic.com/r/987b61/9 – Akki

+0

我只想選擇複選框,如果金額欄的值等於100000 – Akki

回答

1

關閉我的頭頂,我會嘗試

topElement = driver.findElement(By.xpath("(//table[@width=760][@cellspacing=1])[2]/tbody")); 

List<WebElement> potentialElements = topElement.findElements(By.xpath("//*[contains(@value,'100000')]")); 

if (potentialElements.size() > 0) { 
    //You have caught your fish 
} 

這是更有效的讓硒做搜索,一旦找到與每個元素並在循環中測試它的值。

編輯

問題陳述:我想選擇「選擇」列中的複選框,如果有一個「amount'which等於在相關的10萬‘量’一欄。

public void checkPaymentItemsEqualTo100k() throws InterruptedException{ 
    topElement = driver.findElement(By.xpath("(//table[@width=760][@cellspacing=1])[2]/tbody")); 
    List<WebElement> potentialElements = topElement.findElements(By.xpath("//*[contains(@value,'100000')]")); 

    if (potentialElements.size() > 0) { 
     for (WebElement x: potentialElements) { 

      WebElement grandParent = x.findElement(By.xpath("/../../.."); 
      WebElement wElement = grandParent.findElement(By.xpath("/td[1]/div/input[@name='bulk_CHKpayment'])[1]")); 
      if(wElement.getAttribute("onclick") == null){ 
       JavascriptExecutor executor = (JavascriptExecutor) driver; 
       executor.executeScript ("arguments[0].click();" , wElement); 
       Log.info("Payment Item equal to 100k Amount is available"); 
       sleepResponsibly(2000); 
      } 
     } 
    } 
} 

public void sleepResponsibly(int timeMillisecond){ 
    try{ 
     Thread.sleep(timeMillisecond); 
    } catch (InterruptedException ex) { 
     Thread.currentThread().interrupt(); 
     throw new RuntimeException(ex); 
    } 
} 

警告:我沒有測試過這個。

+0

感謝您的答案。你能爲我寫一個完整的函數嗎?我有點困惑。 – Akki

+0

如果你必須睡覺,然後負責任地睡覺。 – MikeJRamsey56

+0

爺爺奶奶在這裏做什麼? – Akki

0

它更容易做xpath。 實際的Xpath可能會有所不同,因爲我沒有更改測試。但想法是你找到與文本匹配的TD,然後轉到父元素,這是ROW,然後第一個TD然後輸入。

public void selectValue(String value){ 
    String xpath = "//td[contains(text(),'"+value+"')]/../td[1]/input"; 
    WebElement checkbox = driver.findElement(By.xpath(xpath)); 
    checkbox.click(); 

} 
相關問題