0

我正在尝试单击表格中的复选框。我搜索表格,当我在第二列中找到正确的信息时,我想单击该行第一列中的复选框

    public WebDriver searchSelectStore(WebDriver dr1, String srchTable, String srchFor) {
 // Grab the table 
    WebElement table = dr1.findElement(By.id(srchTable)); 

    // Now get all the TR elements from the table 
    List<WebElement> allRows = table.findElements(By.tagName("tr")); 

    // And iterate over them, getting the cells 
    for (WebElement row : allRows) { 
        List<WebElement> cells = row.findElements(By.tagName("td")); 

    for (WebElement cell : cells) { 
        // 

         if(cell.getText().equals(srchFor)){ 


              System.out.println(row.getText());

                   // here I need to click on the first column of that row



             }

        }
4

1 回答 1

0

实际上,我会稍微重写您的代码,因为您说您总是检查第二个单元格中的文本,然后在找到时单击第一个单元格,我会执行以下操作:

List<WebElement> allRows = table.findElements(By.tagName("tr"));  
for (WebElement row : allRows) {
    if (row.findElement(By.xpath("./td[2]").getText().equals(srchFor)){
        row.findElement(By.xpath("./td[1]").click();
    }
}
于 2013-06-12T13:12:34.353 回答