在 Selenium WebDriver 的所有页面中搜索某个元素(分页)

问题描述 投票:0回答:1

我需要在所有页面的表格中搜索特定文本。假设我必须搜索文本(例如:“xxx”),并且该文本出现在第 3 页表格的第 5 行。 我尝试过一些代码:

List<WebElement> allrows = table.findElements(By.xpath("//div[@id='table']/table/tbody/tr"));
List<WebElement> allpages = driver.findElements(By.xpath("//div[@id='page-navigation']//a"));
    System.out.println("Total pages :" +allpages.size());
    for(int i=0; i<=(allpages.size()); i++)
        {
            for(int row=1; row<=allrows.size(); row++)
                {
                    System.out.println("Total rows :" +allrows.size()); 
                    String name = driver.findElement(By.xpath("//div[@id='table']/table/tbody/tr["+row+"]/td[1]")).getText();
                    //System.out.println(name);
                    System.out.println("Row loop");
                    if(name.contains("xxxx"))
                        {
                            WebElement editbutton = table.findElement(By.xpath("//div[@id='table']/table/tbody/tr["+row+"]/td[3]"));
                            editbutton.click();
                            break;
                        }
                    else
                    {
                        System.out.println("Element doesn't exist");
                    }
                    allpages = driver.findElements(By.xpath("//div[@id='page-navigation']//a"));
                }

            allpages = driver.findElements(By.xpath("//div[@id='page-navigation']//a"));
            driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
            allpages.get(i).click();
        }

抱歉,我错过了描述错误。这段代码得到正确执行,它检查每个页面的每一行上的元素“xxx”,并在找到时单击编辑按钮。

之后它移动到 “allpages.get(i).click();” // 代码用于点击页面

但是找不到任何分页,因此显示错误“元素在点 (893, 731) 处不可点击。其他元素将收到点击......”

java selenium-webdriver junit4
1个回答
0
投票

对于每个页面循环,您使用一个表 WebElement 对象。所以我假设进入下一页后您会得到 StaleElementReferenceException。我想解决方案可能是在每个页面循环上定义表。将此行

List<WebElement> allrows = table.findElements(By.xpath("//div[@id='table']/table/tbody/tr"));
也移到
for(int i=0; i<=(allpages.size()); i++)
之后

编辑:顺便说一句,在这一行

allpages.get(i).click()
我认为您必须单击下一页链接,而不是看起来的当前页面链接

© www.soinside.com 2019 - 2024. All rights reserved.