单击活动行第一列中的复选框

时间:2013-06-12 12:39:24

标签: java selenium select checkbox html-table

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

    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



             }

        }

1 个答案:

答案 0 :(得分: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();
    }
}