简体   繁体   English

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

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

My project has one dynamic table. 我的项目有一个动态表。 I need to go to a particular cell and click on an available link. 我需要转到特定的单元格,然后单击可用链接。 I had reached a particular cell but am unable to click on the link which appears in the table cell. 我已到达特定的单元格,但无法单击表格单元格中出现的链接。

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

Output: 输出:

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

On a single pass through your code, I found the following issue 通过您的代码,我发现了以下问题

Change the following 改变以下

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

to

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

On second thoughts, I have some suggestions on your code :- 再次考虑,我对您的代码提出了一些建议:

  1. Instead of finding the table as a whole, you can find the tbody since your data under test lies here. 无需查找整个表,而是可以找到正文,因为被测数据位于此处。
  2. I believe, you know in which column the project name. 我相信,您知道项目名称在哪一列。 So you can avoid the loop for iterating through the List rowCells. 因此,您可以避免循环遍历List rowCells的循环。 Instead you can directly use rowCells.get(index) to get the exact column(index starts from 0. If your project name is in column 2, then index =1). 相反,您可以直接使用rowCells.get(index)获取确切的列(索引从0开始。如果您的项目名称在第2列中,则index = 1)。
  3. The same is applicable for the column which contains the link to click(). 这同样适用于包含click()链接的列。 Use rowCell.get(index) to get the column and then click on it. 使用rowCell.get(index)获取列,然后单击它。

So your code can be modified as follows :- 因此,您的代码可以如下修改:

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

Let me know if this helps you. 让我知道这是否对您有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用带有 Java 的 Selenium WebDriver 单击按钮? - How can I click on a button using Selenium WebDriver with Java? 如何使用 Selenium Webdriver 和 Java 在同一行中单击旁边/上一个表格单元格(TD)? - How to click on the aside / previous Table Cell (TD) in the same row using Selenium Webdriver with Java? Selenium Webdriver Java:如何使用行号和列号单击表中的特定单元格 - Selenium Webdriver Java: How to click on a particular cell in table using row and column numbers 为什么我不能在Java中使用Selenium Webdriver截屏? - Why I can't take a screenshot using selenium webdriver in java? Selenium webdriver无法单击页面外的链接 - Selenium webdriver can't click on a link outside the page 如何使用Selenium Webdriver 2.25-Java单击ahref链接? - How to click on the ahref link using selenium webdriver 2.25 - java? 如何<a class </a>在Selenium WebDriver中使用Java</a>单击带有标签名称的链接 - How to click a link with Tag name <a class </a> using Java in Selenium WebDriver 使用带有Java的Selenium WebDriver无法单击鼠标悬停链接 - Unable to click a mouse hover link using Selenium WebDriver with Java 如何使用Selenium WebDriver(Java)单击图像/链接 - How to click on an image/link using Selenium WebDriver (Java) 无法使用Selenium Webdriver(Java)单击“确认电子邮件”链接 - Not able to click on “Confirm Email” link using selenium webdriver (Java)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM