簡體   English   中英

我無法使用Java在Selenium WebDriver中的動態表格單元格中單擊鏈接

[英]I can't click a link in a dynamic table cell in Selenium WebDriver using Java

我的項目有一個動態表。 我需要轉到特定的單元格,然后單擊可用鏈接。 我已到達特定的單元格,但無法單擊表格單元格中出現的鏈接。

@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

通過您的代碼,我發現了以下問題

改變以下

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. 無需查找整個表,而是可以找到正文,因為被測數據位於此處。
  2. 我相信,您知道項目名稱在哪一列。 因此,您可以避免循環遍歷List rowCells的循環。 相反,您可以直接使用rowCells.get(index)獲取確切的列(索引從0開始。如果您的項目名稱在第2列中,則index = 1)。
  3. 這同樣適用於包含click()鏈接的列。 使用rowCell.get(index)獲取列,然后單擊它。

因此,您的代碼可以如下修改:

@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();                }
    }
}

讓我知道這是否對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM