Matas Vaitkevicius
Matas Vaitkevicius

Reputation: 61459

How to overcome Selenium CSS parent only CSS selector limitation

Is there a known solution to do CSS selectors like this one in Selenium?

$('.sss').find('>div.ui-collapsible-content')

works fine on Chrome developer tools.

In Selnenium it throws exception

OpenQA.Selenium.InvalidElementStateException : invalid element state: Failed to execute    'querySelector' on 'Element': '>div.ui-collapsible-content' is not a valid selector.
(Session info: chrome=33.0.1750.154)
(Driver info: chromedriver=2.8.241075,platform=Windows NT 6.1 SP1 x86)

I could just use selector without parent part

.find('div.ui-collapsible-content')

and only take first element, but this is somewhat hacky solution and someone must have ran into similar situation by now and found more elegant solution than this.

jsfiddle HTML example - http://jsfiddle.net/uHYZL/1/

Upvotes: 1

Views: 363

Answers (2)

Robbie Wareham
Robbie Wareham

Reputation: 3438

Have you tried using Xpath? It would look pretty horrible and complex but I am sure you could construct a selector which would only find parents, then continue from there.

i would try and construct one but don't have access to a computer currently

Upvotes: 0

Yi Zeng
Yi Zeng

Reputation: 32865

I wouldn't call this a limitation. This looks like expected behaviour to me, as I don't see >div.ui-collapsible-content being a valid CSS selector in the first place.

The reason $('.sss').find('>div.ui-collapsible-content') works is because you are using jQuery, which uses Sizzle as the CSS selecting engine under the hood. It supports things that W3C spec doesn't support, like :contains() etc.

I'm not sure what you are trying to achieve here. Wouldn't something like below suit you needs?

var content = driver.FindElement(By.CssSelector(".sss > div.ui-collapsible-content"));

Upvotes: 2

Related Questions