我需要在元素中查找文本并断言它是否与我所需的结果匹配。
问题是,页面中可以有 n 个从 1 到 100 的元素。所以我无法获取所有这些元素的 xpath,然后断言其中的文本。
xpath 看起来像这样:(从第一个元素开始)
(//DIV[@class='issues-list-item clearfix'])[1]
(//DIV[@class='issues-list-item clearfix'])[2]
(//DIV[@class='issues-list-item clearfix'])[3]
(//DIV[@class='issues-list-item clearfix'])[4]
....
(//DIV[@class='issues-list-item clearfix'])[100]
如何循环遍历这些 xpath 并断言我的文本?
我引用了几篇文章后尝试了以下方法,但确实没有帮助。
private static WebElement element = null;
private static List<WebElement> elements = null;
public WebElement test() throws Exception {
elements = driver.findElements(By.xpath("(//DIV[@class='issues-list-item clearfix'])[1]"));
for (WebElement element : elements) {
List<WebElement> TE = element.findElements(By.xpath("(//DIV[@class='issues-list-item clearfix'])[1]"));
if (TE.size() > 0) {
String myText = TE.get(0).getText();
if (myText.contains("High")) {
return element;
}
}
}
return null;
最佳答案
你可以试试这个:
public List<WebElement> test() throws Exception {
List<WebElement> TE = new ArrayList<WebElement>();
elements = driver.findElements(By.xpath("(//DIV[@class='issues-list-item clearfix'])"));
for (WebElement element : elements) {
if(element.getText().contains("High")) {
TE.add(element);
}
}
return TE;
}
请注意,它将返回一个列表 Web 元素,其中包含 High
作为文本。
关于java - 从多个 xpath 中提取文本并断言文本 - Selenium/Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51213323/