1

Webdriver how to find the parent field set element of a given element using Xpat...

 2 years ago
source link: https://www.codesd.com/item/webdriver-how-to-find-the-parent-field-set-element-of-a-given-element-using-xpath.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Webdriver how to find the parent field set element of a given element using Xpath

advertisements

I have a HTML like the one below

<fieldset id='fs1'>
  <legend>Basic Details</legend>
  <div>
    <input type='text' id='txt1'/>
    <input type='text' id='txt2'/>
  </div>
</fieldset>

I can get to the textboxes by saying

driver.findElement(By.id("txt1"));

Now, I want my webdriver to check if this textbox (or any other element at this stage) is the child of a fieldset. In other words, I want to find the parent "fieldset" tag of a given element.

And I need to use xpath and nothing else.

Now, I have tried using ("..") as my xpath query, which will return me the Div parent, and i have to keep doing it until I hit the fieldset. I just wonder if there is a straight forward solution. Can anyone suggest an xpath query which will help me do what I have explained?

Thanks, Sriram


You're looking for the ancestor:: axis, which includes the parent, grandparent, etc. all the way up the tree. In particular:

ancestor::fieldset[1]

will give you the nearest enclosing fieldset element ancestor for the current context element (or an empty node set if the context element isn't inside a fieldset).


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK