Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to go back to the immediate parent in XPath and get the text?

My xpath is

//div[contains(@class,'ui-dialog ui-widget ui-widget-content ui-corner-all ui-front ui-draggable')]//div[@class='ui-widget-content slick-row odd' or @class='ui-widget-content slick-row even']/div/input[contains(@id,'custProglabel')]

I need to access div just before input tag and get its text. I tried using:

By.Xpath(//input[contains(@id,'custProglabel')]/preceding-sibling::div).getText();

I have many elements like this and only the content of my input tag changes. So based on the content I am trying to access the immediate div and get its text.

like image 235
Aish Avatar asked Apr 13 '16 18:04

Aish


People also ask

How do I go back to XPath element?

Backward Traversing: There are the cases when we need to traverse backward in the XML Xpath. IN the XPath we can traverse backward using the double dots(..). We can traverse backward as many levels as we want like ../../… For example, if we want to find the parent of any child div selector then we can use it as.

How do I navigate to parent node in XPath?

A Parent of a context node is selected Flat element. A string of elements is normally separated by a slash in an XPath statement. You can pick the parent element by inserting two periods “..” where an element would typically be. The parent of the element to the left of the double period will be selected.

How do you navigate to parent in Selenium?

We can select the parent element of a known element with Selenium webdriver. First of all we have to identify the known element with the help of any of the locators like id, classname and so on. Then we have to identify its parent element with findElement(By. xpath()) method.


1 Answers

You can get immediate parent using ..

By.xpath("//input[contains(@id,'custProglabel')]/..")

As a side note, xpath should start with lower case 'x'.

like image 100
Guy Avatar answered Nov 04 '22 12:11

Guy