Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use OR Condition in selenium findElements() method for any selector?

I want to get all WebElement information having class name "act" or "dact"

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"));

Thanks!

like image 296
OverrockSTAR Avatar asked Mar 19 '11 12:03

OverrockSTAR


People also ask

How to use findelements command in selenium?

FindElements in Selenium command takes in By object as the parameter and returns a list of web elements. It returns an empty list if there are no elements found using the given locator strategy and locator value. Below is the syntax of find elements command. List<WebElement> elementName = driver.findElements (By.LocatorStrategy ("LocatorValue"));

What is the use of find element in selenium?

Find Element command returns the web element that matches the first most element within the web page. Find Elements command returns a list of web elements that match the criteria. Find Element by XPath in Selenium command throws NoSuchElementException if it does not find the element matching the criteria.

How to find element using CSS selector in selenium?

CSS Selector is used to provide style rules for web pages and also can be used to identify one or more web elements. Example: Consider the same search box of the Amazon page. It has a class attribute whose value is nav-search-input. Using the same value, the Selenium findElement method can locate the element. 5. Find By XPath

What is the use of findelement by method in JavaScript?

findElement (By by) method finds and returns the first matching element within the current context by the given mechanism. For example if we pass id in above method then it will find and return the first element whose id is a match. If no web element is found with the provided mechanism then it throws NoSuchElementException.


2 Answers

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);

Alternatively, you could use an xpath locator as suggested by @Alejandro

List<WebElement> all = driver.findElements(By.xpath("//*[@class='act' or @class='dact']"));
like image 61
Dave Hunt Avatar answered Oct 02 '22 12:10

Dave Hunt


I know this question is really old but probably the easiest way to do this is to use a CSS selector like

driver.findElements(By.cssSelector(".act, .dact"));

You should prefer CSS selectors to XPath because the browser support is better and they are more performant (and I think they are easier to learn/understand). CSS selectors can do everything By.className() can plus a whole lot more. They are very powerful and very useful.

Some CSS selector references to get you started.

W3C CSS Selector Reference

Sauce Labs CSS Selector Tips

like image 24
JeffC Avatar answered Oct 02 '22 12:10

JeffC