我无法使用Java单击Selenium WebDriver中动态表格单元格中的链接

时间:2014-03-28 12:06:32

标签: java selenium selenium-webdriver

我的项目有一个动态表。我需要转到特定的单元格并单击可用链接。我到达了一个特定的单元格,但无法点击表格单元格中显示的链接。

@Test(priority = 1)
public void projectDelete() throws Exception {
    int rowCount = -1;
    int columnCount = 0;
    WebElement table = webdriver.findElement(By.id("projectList"));
    List<WebElement> allRows = table.findElements(By.tagName("tr"));
    for (WebElement row : allRows) {
        rowCount++;
        List<WebElement> rowCells = row.findElements(By.tagName("td"));
        for (WebElement cell : rowCells) {
            columnCount++;

            String projectName = cell.getText();

            if (projectName.equals("TEST1")) {
                System.out.println("Table Data" + cell.getText());
                System.out.println("Table Row " + rowCount);

                System.out.println("TEST PROJECT LINE FOUND ..... "
                        + rowCount);

                webdriver.findElement(By.xpath("//*[@id='projectList']/tbody/tr[rowCount]/td[5]")).click();
                webdriver.findElement(By.xpath("//*[@id='493']")).click();
            }
        }
        columnCount = 0;
    }
}

输出:

Table DataTEST1
Table Row 76
TEST PROJECT LINE FOUND ..... 76
FAILED: projectDelete
org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//*[@id='projectList']/tbody/tr[rowCount]/td[5]"}
Command duration or timeout: 20.06 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html

1 个答案:

答案 0 :(得分:0)

在单次传递代码时,我发现了以下问题

更改以下内容

webdriver.findElement(By.xpath("//*[@id='projectList']/tbody/tr[rowCount]/td[5]")).click();

webdriver.findElement(By.xpath("//*[@id='projectList']/tbody/tr["+rowCount+"]/td[5]")).click();

再想一想,我对你的代码有一些建议: -

  1. 您可以找到tbody,而不是找到整个表格,因为您的测试数据就在这里。
  2. 我相信,你知道项目名称在哪一列。因此,您可以避免循环迭代List rowCells。相反,您可以直接使用rowCells.get(index)来获取精确列(索引从0开始。如果您的项目名称在第2列,则index = 1)。
  3. 同样适用于包含click()链接的列。使用rowCell.get(index)获取列,然后单击它。
  4. 因此您的代码可以修改如下: -

    @Test(priority = 1)
    public void projectDelete() throws Exception {
        //find tbody
        WebElement table = webdriver.findElement(By.xpath("/table[@id='projectList']/tbody"));
    
        //get all rows
        List<WebElement> allRows = table.findElements(By.tagName("tr"));
    
        //iterate through the rows
        for (WebElement row : allRows) {
                //get the rowCells in each row
                List<WebElement> rowCells = row.findElements(By.tagName("td"));
    
                //get the column which contains the project name and get text
                String projectName = rowCells.get(indexofColumnwhichhasProjectname).getText();
    
                //Compare if the project name equals TEST1
                if (projectName.equals("TEST1")) {
                    System.out.println("Table Data : " + projectName);
                    System.out.println("Table Row : " + rowCells.indexOf(projectName));
    
                    //get the column containing the link and click on it.
                    rowCells.get(4).click();
    
                    //webdriver.findElement(By.id("493")).click();
                    //Img is contained within the row containing the project Name
                    //So, find the Img in the row and click
                    row.findElements(By.cssSelector("img[alt='Delete Project']")).click();                }
        }
    }
    

    如果这有助于您,请告诉我。