如何通过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
How to find parent elements by python webdriver?
提问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?
ordriver.find_element_next?
ordriver.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:
在这里它将:
- Firstly navigate to
android.widget.TextView[@text = "Current element text"]
- Then it will go back to parent element i.e
android.widget.RelativeLayout
and select the nextandroid.widget.TextView[@text="Next element text"]
under the same hierarchy.
- 首先导航到
android.widget.TextView[@text = "Current element text"]
- 然后它将返回到父元素,即
android.widget.RelativeLayout
选择android.widget.TextView[@text="Next element text"]
同一层次结构下的下一个元素。