user1593846
user1593846

Reputation: 756

Multiple values in css selector with Java and selenium

I need to get all elements with a class inside a wrapper div. I have done this before with php and the css selector would look something like this:

$this->elements($this->using('css selector')->value('div.active tr[class="theRow"]'));

Now this would give me all foo elements inside the wrapper active but I dont know how to do it with Java. I want a list with all the webElements like so:

List<WebElement> list = driver.findElements(By.cssSelector(".active,.theRow"));

This however will give me all theRow elements, eaven those outside the active wrapper. any sugestions?

the code below also give all theRow elements as expected:

List<WebElement> list = driver.findElements(By.className("theRow"));    

but this gives me a empty list

List<WebElement> list = driver.findElements(By.cssSelector("tr[class='row-hover']"));       

Upvotes: 0

Views: 9407

Answers (1)

Robbie Wareham
Robbie Wareham

Reputation: 3448

Is there a chance the class attribute of the span contains multiple values?

If so, then you may have problems using 'class=' as it would not match elements which have two or more classes.

If so, then try this;

List<WebElement> list = driver.findElements(By.cssSelector("div.active span.foo"));

When ever I access class outside of ".", I would always use "[class*='foo']" because you cannot guarantee the order in which multiple values appear in the class attribute, but would always use the "." notation where possible. However, whenever I use "class" in Xpath I always use "[contains(@class,'foo')] because Xpath always treats "class" as a string literal, where as CSS "." can cope with multiple values

Upvotes: 2

Related Questions