Devegnik
Devegnik

Reputation: 195

Xpath up to parent element using nightwatch

I have this structure:

 <div class="class">
    <h4 class="class2"> 
      "Condition" </h4>
    <div class = "click_class">Click_text  </div>
 </div>

I heed to click on the element with class ="click_class" if the h4 text == "Condition".

I try Xpath:

 .useXpath()
 .click('//div[contains(@class, "class")]//h4[text()[contains(.,"Condition")]

It is works. I found the h4 with text == "Condition". Now, in my opinion, I need to go to parent class and click on click_class.

.click('//div[contains(@class, "class")]//h4[text()[contains(.,"Condition")]..//div[text()="Click_text"]')

but it is not works.

How can I click on the element with text with condition in h4?

Upvotes: 2

Views: 1974

Answers (2)

har07
har07

Reputation: 89285

Alternatively, you can put the expression that check for text content of h4 in a predicate for the parent div instead :

//div[contains(@class, "class")][h4/text()[contains(.,"Condition")]]

and then navigate to return child div element that have class "click_class" :

/div[contains(@class, "click_class")]

So the entire expression would be as follows (wrapped for readability) :

//div[contains(@class, "class")][h4/text()[contains(.,"Condition")]]
/div[contains(@class, "click_class")]

demo

Upvotes: 1

Andersson
Andersson

Reputation: 52665

Try below XPath:

//h4[normalize-space(text())="Condition"]/following-sibling::div[@class="click_class"]

Upvotes: 1

Related Questions