Daniel Schissler
Daniel Schissler

Reputation: 125

Nested predicates in Xpath

enter image description here

I am trying to access the div node via

//div[@data-full='2018-1-15']

Normally I would just search by Xpath and grab this node. However the nature of the site is that there are a number of nodes with this property, and only one is clickable.

Because of this I have to grab the

//div[@class='dw-cal-slide dw-cal-slide-a'] 

node first and then step down. I know I'm trying to do something like this:

Step down one node:

//div[@class='dw-cal-slide dw-cal-slide-a']/div/

And then search for child nodes that have a child node of their own with the property

//div[@data-full='2018-1-15']. 

Having trouble with the syntax. Any help would be great!

Upvotes: 0

Views: 607

Answers (2)

Andersson
Andersson

Reputation: 52675

Try

//div[contains(@class, 'dw-cal-slide') and contains(@class, 'dw-cal-slide-a')]//div[@data-full='2018-1-15']

But IMHO it's better (shorter expression) to use CSS selector

div.dw-cal-slide.dw-cal-slide-a div[data-full='2018-1-15']

If you want to locate ancestor and descendant div in two code lines, then you can use (Python example)

ancestor = driver.find_element_by_xpath("//div[contains(@class, 'dw-cal-slide') and contains(@class, 'dw-cal-slide-a')]")

and

descendant = ancestor.find_element_by_xpath(".//div[@data-full='2018-1-15']")

Upvotes: 2

Vikrant Ojha
Vikrant Ojha

Reputation: 36

As you said, there are multiple node with same property and only one is clickable. you can click on the node by checking isEnable. other thing you can try with following-sibling or preceding-sibling. example can be found on below stackoverflow link:

How to use XPath preceding-sibling correctly

Upvotes: 0

Related Questions