Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get grandParent using xpath in selenium webdriver

<div class="myclass">
  <span>...</span>
  <div>
       <table>
              <colgroup>...</>
              <tbody>
                    <tr>...</tr>
                    <tr>
                         <td> 
                              <input ...name="myname" ...> 
                         </td>
                    </tr>
                    <tr>...</tr>
              <tbody>
       </table>
  </div>

</div>

this is my html code ... I have attribute "name" ..so I can access tag ...but from this line I want to access top element. One way is that I write driver.find_element_by_xpath('..') 6-7 times to get that parent but I dont know how many steps I have to go above. I simply want a xpath expression or similar thing to access top .

I'm using Selenium webdriver with python

like image 893
pankaj udaas Avatar asked Nov 30 '22 02:11

pankaj udaas


1 Answers

Instead of targeting a deeper level element and going up in the tree, you can select the higher level element and test for a descendant node attribute:

.//div[@class="myclass"][.//input[@name="myname"]]
like image 165
paul trmbrth Avatar answered Dec 05 '22 13:12

paul trmbrth