我能';t使用Java在SeleniumWebDriver中单击动态表单元格中的链接

我能';t使用Java在SeleniumWebDriver中单击动态表单元格中的链接,java,selenium,selenium-webdriver,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")); Li

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

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

在对代码进行一次检查时,我发现了以下问题

更改以下内容

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

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

  • 您可以找到tbody,而不是作为一个整体查找表,因为您的测试数据位于此处
  • 我相信,你知道项目名称在哪一列。因此,可以避免循环遍历列表行单元格。相反,您可以直接使用rowCells.get(index)来获取确切的列(索引从0开始。如果项目名称位于列2中,则index=1)
  • 这同样适用于包含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();                }
        }
    }
    
    @测试(优先级=1)
    public void projectDelete()引发异常{
    //找到尸体
    WebElement table=webdriver.findElement(By.xpath(“/table[@id='projectList']/tbody”);
    //获取所有行
    List allRows=table.findElements(按.tagName(“tr”));
    //遍历行
    for(WebElement行:所有行){
    //获取每行中的行单元格
    列表行单元格=行.findElements(按.tagName(“td”));
    //获取包含项目名称的列并获取文本
    String projectName=rowCells.get(indexofcolumn,其中包含projectName.getText();
    //比较项目名称是否等于TEST1
    if(projectName.equals(“TEST1”)){
    System.out.println(“表数据:“+projectName”);
    System.out.println(“表行:“+rowCells.indexOf(projectName));
    //获取包含链接的列并单击它。
    获取(4)。单击();
    //webdriver.findElement(By.id(“493”))。单击();
    //Img包含在包含项目名称的行中
    //因此,在该行中找到Img并单击
    row.findElements(By.cssSelector(“img[alt='Delete Project']”)。单击();}
    }
    }
    

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

    如果可能,请发布HTML代码。关闭。您正在将
    行数
    封装在引号中,而您不应该这样做。丢失答案很好。。。但是line@webdriver.findElement(By.id(“493”)).click();id(“493”)每次都在变化。如何控制这一点?那么我建议您使用xpath而不考虑id。或者您可以使用css选择器。img[alt='Delete Project'](假设alt属性为常量:)row.findElement(通过.cssSelector(“img[alt='Delete Project'])。单击();好的。。。。
    webdriver.findElement(By.xpath("//*[@id='projectList']/tbody/tr["+rowCount+"]/td[5]")).click();
    
    @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();                }
        }
    }