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

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


You can get immediate parent using ..

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

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


Rather than drop down to input and come back up to div, considerusing a predicate on div

//div[input/@id='custProglabel']

or any element

//*[input/@id='custProglabel']

Also, note that contains() could mistakenly match custProglabel2 or MYcustProglabel, so it's better to use equality test here.


use below xpath:

//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')]/parent::div