简体   繁体   English

使用 xpath selenium python 获取子元素

[英]Get child element using xpath selenium python

We can get a parent of a selenium element using xpath, by我们可以使用 xpath 获得 selenium 元素的父元素,通过

par_element = element.find_element_by_xpath('..')

In a similar fashion, how can we get the child of the element?以类似的方式,我们如何获得元素的子元素? I tried the following and did not work我尝试了以下但没有工作

child_element = element.find_element_by_xpath('/')
child_element = element.find_element_by_xpath('//')

To get to the child of the WebElement you need to set the context to the current element using the dot character ie .要访问WebElement的子元素,您需要使用点字符 ie 将上下文设置为当前元素. . . So effectively your line of code can be either of the following:因此,您的代码行可以是以下任何一种:

child_element = element.find_element_by_xpath('./')

or或者

child_element = element.find_element_by_xpath('.//')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM