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
 

posted @ 2020-06-11 17:33  杭州-小海  阅读(2329)  评论(0编辑  收藏  举报