如何通过python webdriver查找父元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18079765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:52:58  来源:igfitidea点击:

How to find parent elements by python webdriver?

pythonseleniumxpathselenium-webdriverparent-child

提问by Stella

Is there any methods for python+selenium to find parent elements, brother elements, or child elements just like

python+selenium 有没有什么方法可以找到父元素、兄弟元素或子元素,就像

driver.find_element_parent?or
driver.find_element_next?or
driver.find_element_previous?

driver.find_element_parent?
driver.find_element_next?
driver.find_element_previous?

eg:

例如:

<tr>
  <td> 
     <select>
        <option value=0, selected='selected'> </option> 
        <option value=1, > </option>
        <option value=2,> </option>
     </select>
   </td>
   <td> 'abcd'
     <input name='A'> </input>
    <td>
<tr>

I've tried like below, but fail:

我试过如下,但失败了:

input_el=driver.find_element_by_name('A')
td_p_input=find_element_by_xpath('ancestor::input')

How can I get the parent of input elementand then, finally, get the option selected?

如何获取输入元素的父元素,然后最后选择选项

采纳答案by alecxe

You can find a parent element by using ..xpath:

您可以使用..xpath查找父元素:

input_el = driver.find_element_by_name('A')
td_p_input = input_el.find_element_by_xpath('..')


What about making a separate xpath for getting selected option, like this:

为选择选项制作一个单独的 xpath 怎么样,像这样:

selected_option = driver.find_element_by_xpath('//option[@selected="selected"]')

回答by drkthng

From your example I figure you only want the selected option within a table-row if and only if this row also has an input element with the name "A", no matter where in the html-tree this element resides below the row-element.

从您的示例中,我认为您只需要表格行中的选定选项,当且仅当该行也有一个名为“A”的输入元素时,无论该元素在 html-tree 中的哪个位置位于行元素下方.

You can achieve this via the xpath ancestor-axis.

您可以通过 xpath 祖先轴实现此目的。

For the sake of better readability I will show how to do this step by step (but you can actually put everything in only one xpath-expression):

为了更好的可读性,我将逐步展示如何执行此操作(但实际上您可以将所有内容都放在一个 xpath 表达式中):

// first find your "A" named element
namedInput = driver.find_element_by_name("A");

// from there find all ancestors (parents, grandparents,...) that are a table row 'tr'
rowElement = namedInput.find_element_by_xpath(".//ancestor::tr");

// from there find the first "selected" tagged option
selectedOption = rowElement.find_element_by_xpath(".//option[@selected='selected']");

回答by Parth Naik

One of the possible ways to navigate to element under same hierarchy is to use /../in xpath as shown below:

导航到同一层次结构下的元素的一种可能方法是/../在 xpath 中使用,如下所示:

current_element = driver.find_element_by_xpath('//android.view.ViewGroup/android.widget.RelativeLayout/android.widget.TextView[@text="Current element text"]/../android.widget.TextView[@text="Next element text"]')

Here it will:

在这里它将:

  1. Firstly navigate to android.widget.TextView[@text = "Current element text"]
  2. Then it will go back to parent element i.e android.widget.RelativeLayoutand select the next android.widget.TextView[@text="Next element text"]under the same hierarchy.
  1. 首先导航到 android.widget.TextView[@text = "Current element text"]
  2. 然后它将返回到父元素,即android.widget.RelativeLayout选择android.widget.TextView[@text="Next element text"]同一层次结构下的下一个元素。