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.
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.
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.
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.
You can get immediate parent using ..
By.xpath("//input[contains(@id,'custProglabel')]/..")
As a side note, xpath
should start with lower case 'x'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With