selenium 查找父子节点和兄弟节点

<html>
<body>
<div id="parent">
    parent
    <div id="A"> old brother</div>
    <div id="B"> child</div>
    <div id="C"> litter brother</div>
</div>
</body>
</html>

这里我主要介绍xpath,我认为这个最好用,通用

父查子

print driver.find_element_by_xpath("//div[@id='parent']/div[2]").text  #child
print driver.find_element_by_xpath("//div[@id='parent']/div[@id='B']").text  #child

子查父

print driver.find_element_by_xpath("//div[@id='B']/..").text #parent

定位兄弟节点

print driver.find_element_by_xpath("//div[@id='B']/../div[1]").text #old brother
print driver.find_element_by_xpath("//div[@id='B']/../div[3]").text #litter brother

猜你喜欢

转载自blog.csdn.net/weixin_41858542/article/details/85068645