在Selenium WebDriver中使用xpath获取grandParent [英] Get grandParent using xpath in selenium webdriver

查看:115
本文介绍了在Selenium WebDriver中使用xpath获取grandParent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

</div>

这是我的html代码...我具有属性名称" ..so,因此我可以访问标签...但是从这一行中,我想访问顶部元素. 一种方法是我写6-7次driver.find_element_by_xpath('..')以获得那个父母,但我不知道我必须经过多少步骤.我只是想要一个xpath表达式或类似的东西来访问top.

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 .

我正在将Selenium Webdriver与python一起使用

I'm using Selenium webdriver with python

推荐答案

您可以选择更高级别的元素并测试后代节点属性,而不是定位到更深层次的元素并在树中向上移动:

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"]]

这篇关于在Selenium WebDriver中使用xpath获取grandParent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆