首页 > 解决方案 > 如何在硒的动态表中的所有页面中搜索包含特定文本的元素

问题描述

目前,我搜索文本并单击使用自定义 xpath 在动态表中具有搜索字符串的行中的按钮: //div/span[contains(text(),'search string')]/parent::div/parent::td/following-sibling::td/button[@class='btton-clicked-on-table']")
如果搜索字符串在第一页上,则该按钮有效。

现在,如果相同的字符串在第一页以外的另一页上,那么我找不到异常。那么如何在动态表的多个页面中搜索相同的字符串呢?

标签: javaselenium-webdriver

解决方案


鉴于您提供的示例 URL,这里有一些快速代码来执行您正在寻找的内容,您可以适应您正在使用的网站。

String url = "https://www.redmine.org/projects/redmine/issues";
driver.navigate().to(url);

String searchTerm = "Tracker";
By searchTermLocator = By.xpath("//td[@class='subject']/a[contains(.,'" + searchTerm + "')]");
List<WebElement> links = driver.findElements(searchTermLocator);
while (links.size() == 0)
{
    // click Next
    driver.findElement(By.linkText("Next »")).click();
    // look for matches
    links = driver.findElements(searchTermLocator);
}

// match found, click it
links.get(0).click();

我要查找的搜索词位于第二页。


推荐阅读