我使用Java和Selenium自動執行任務。如何根據表的第二列中的值設置第一列中的複選框?
我想根據第二列中的值是否與我的輸入值匹配來設置複選框(位於表格的第一列)。例如,在下面的代碼片段中,值「Magnus」匹配我的輸入值,所以我想設置與它關聯的複選框。
<table class="cuesTableBg" width="100%" cellspacing="0" border="0" summary="Find List Table Result">
<tbody>
<tr class="cuesTableBg">
<tr class="cuesTableRowEven">
<tr class="cuesTableRowOdd">
<td align="center">
<input class="content-nogroove" type="checkbox" name="result[1].chked" value="true">
<input type="hidden" value="1c62dd7a-097a-d318-df13-75de31f54cb9" name="result[1].col[0].stringVal">
<input type="hidden" value="Magnus" name="result[1].col[1].stringVal">
</td>
<td align="left">
<a class="cuesTextLink" href="userEdit.do?key=1c62dd7a-097a-d318-df13-75de31f54cb9">Magnus</a>
</td>
<td align="left"></td>
<td align="left">Carlsen</td>
<td align="left"></td>
</tr>
<tr class="cuesTableRowEven">
</tbody>
</table>
但是我無法做到。在上述情況下,下面兩行達到目的(如我的輸入值與第二行中的匹配):
WebElement checkbox = driver.findElement(By.xpath("//input[@type = 'checkbox' and @name = 'result[1].chked']"));
checkbox.click();
但它不能被用作所需的值可能不是始終處於第二排。
我嘗試下面的代碼塊,但無濟於事:
List<WebElement> rows = driver.findElements(By.xpath("//table[@summary = 'Find List Table Result']//tr"));
for (WebElement row : rows) {
WebElement userID = row.findElement(By.xpath(".//td[1]"));
if(userID.getText() == "Magnus") {
WebElement checkbox = row.findElement(By.xpath(".//input[@type = 'checkbox']"));
checkbox.click();
break;
}
}
對於它的價值,在第二列文的XPath:
/html/body[@id='mainbody']/table/tbody/tr/td/div[@id='contentautoscroll']/form/table[2]/tbody/tr[3]/td[2]/a
我不知道CSS Selectors。它會幫助嗎?
是的,CSS選擇器必須幫助你:) – plsgogame