nirmus
nirmus

Reputation: 5101

CSS Selector - get element knowing element childs

I want to get tr element which contains span.selected in correct column. I need to do it using one selector.

It finds if element exists:

By.CssSelector("tr[role='row'].jqgrow td[aria-describedby$='Default'] span.selected")

but it return span.selected IWebElement.. How can I get tr[role='row'].jqgrow IWebElement (using CssSelector or XPath)?

Upvotes: 1

Views: 1092

Answers (2)

paul trmbrth
paul trmbrth

Reputation: 20748

Something like this may work with XPath

//tr[@role='row']
    [contains(@class, 'jqgrow')]
    [.//td[ends-with(@aria-describedby, 'Default')][.//span[contains(@class, 'selected')]]]

If elements have multiple classes, it's safer (and less readable) to replace the various contains(@class, 'jqgrow') with (@class and contains(concat(' ', normalize-space(@class), ' '), ' jqgrow ')) (trick borrowed from Python's cssselect)

I don't think you can achieve that with CSS selectors alone.

Upvotes: 3

Nathan Dace
Nathan Dace

Reputation: 1555

I do not believe CSS lets you do that, as you need a parent selector:

CSS selector for "foo that contains bar"?.

You will need to use XPath, which has the ability to select parents based on a child. You can check this post for information on using an XPath to select a parent with a specific child node:

XPath find all elements with specific child node

Upvotes: 2

Related Questions