如何对任何选择器使用selenium findElements()方法中的OR条件? [英] How to use OR Condition in selenium findElements() method for any selector?
问题描述
我想获取所有具有类名称"act"或"dact"的WebElement信息
I want to get all WebElement information having class name "act" or "dact"
我正在使用下面的代码行来获取行为"的所有类信息.有人可以帮我在类名中使用OR条件吗?
I am using below line of code to get all class information for "act". Can any one help me to use OR condition in class name?
List<WebElement> nL2 = driver.findElements(By.className("act"));
类似的东西;这样我就不必为每个类分别写两行.
Something similar to this; so that I don't have to write two separate line for each class.
//this is not working
List<WebElement> nL2 = driver.findElements(By.className("act | dact"));
谢谢!
推荐答案
您可以将两个列表合并吗?
Can you just combine the two lists?
List<WebElement> act = driver.findElements(By.className("act"));
List<WebElement> dact = driver.findElements(By.className("dact"));
List<WebElement> all = new ArrayList<WebElement>();
all.addAll(act);
all.addAll(dact);
或者,您可以使用@Alejandro建议的xpath定位器
Alternatively, you could use an xpath locator as suggested by @Alejandro
List<WebElement> all = driver.findElements(By.xpath("//*[@class='act' or @class='dact']"));
这篇关于如何对任何选择器使用selenium findElements()方法中的OR条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!