[英]Extract text from multiple xpath and assert text - Selenium/Java
I need to find the text in an element and assert whether it matches with my required result. 我需要在元素中找到文本并断言它是否与所需的结果匹配。
The thing is, there can be n number of element from 1-100 in the page. 事实是,页面中1-100个元素之间可以有n个元素。 So I can't get the xpath of all those elements and then assert the text in it.
因此,我无法获取所有这些元素的xpath,然后在其中声明文本。
The xpath looks like this: (from the first element) 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]
How do I loop through these xpath and assert for my text? 如何遍历这些xpath并为我的文本断言?
I tried the below method after referring few articles and it really did not help. 在参考了几篇文章之后,我尝试了以下方法,但实际上并没有帮助。
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;
You can try this : 您可以尝试以下方法:
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;
}
Note that , it will return a list web element which contains High
as text. 请注意,它将返回一个列表Web元素,其中包含
High
作为文本。
The more efficient way to do this is to add your check for "High" to the locator. 更有效的方法是将“高”的支票添加到定位器。 That way you don't have to loop through all the elements to find only the ones you want.
这样,您不必遍历所有元素即可仅查找所需元素。 Your locator does all that work for you and more quickly.
定位器可以为您更快地完成所有工作。 There's also a lot less code.
代码也少了很多。
public List<WebElement> test() throws Exception {
return driver.findElements(By.xpath("(//DIV[@class='issues-list-item clearfix'][contains(.,'High')])"));
}
There are a number of ways you can verify that the desired element is found. 您可以通过多种方法来验证是否找到了所需的元素。 One way would be to use a TestNG
Assert
like 一种方法是使用类似的TestNG
Assert
Assert.assertTrue(test().size() > 0, "Verify an element containing 'High' was found.");
//DIV[@class='issues-list-item clearfix'])[1]
your query is wrong. //DIV[@class='issues-list-item clearfix'])[1]
您的查询错误。 The [1] at the end means that it will select oinly teh first item from all the items matching the query before, aka you will only compare against the first one. 最后的[1]表示它将从之前所有与查询匹配的项目中选择第一个项目,也就是您只会与第一个进行比较。 The second query you won't need, neitehr the if checking for size (optionally before the for loop)
您不需要的第二个查询,如果要检查大小,则忽略if(可选在for循环之前)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.