xpath —— 父子、兄弟、相邻节点定位方式详解
xpath
1. 由父节点定位子节点
最简单的肯定就是由父节点定位子节点了,我们有很多方法可以定位,下面上个例子:
对以下代码:
<html>
<body>
<p id="A">
<!--父节点定位子节点-->
<p id="B">
<p>parent to child</p>
</p>
</p>
</body>
</html>
想要根据 B节点 定位无id的子节点,代码示例如下:
# -*- coding: utf-8 -*-
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('D:\\py\\AutoTestFramework\\src\\others\\test.html')
# 1.串联寻找
print driver.find_element_by_id('B').find_element_by_tag_name('p').text
# 2.xpath父子关系寻找
print driver.find_element_by_xpath("//p[@id='B']/p").text
# 3.css selector父子关系寻找
print driver.find_element_by_css_selector('p#B>p').text
# 4.css selector nth-child
print driver.find_element_by_css_selector('p#B p:nth-child(1)').text
# 5.css selector nth-of-type
print driver.find_element_by_css_selector('p#B p:nth-of-type(1)').text
# 6.xpath轴 child
print driver.find_element_by_xpath("//p[@id='B']/child::p").text
driver.quit()
结果:
parent to child
parent to child
parent to child
parent to child
parent to child
parent to child
第1到第3都是我们熟悉的方法,便不再多言。第4种方法用到了css选择器:nth-child(n)
,该选择器返回第n个节点,该节点为p标签;第5种方法用到了另一个css选择器:nth-of-type(n)
,该选择器返回第n个p标签,注意与上一个选择器的区别;第6种方法用到了xpath轴 child
,这个是xpath默认的轴,可以忽略不写,其实质是跟方法2一样的。
当然,css中还有一些选择器是可以选择父子关系的如last-child
、nth-last-child
等,感兴趣可以自行百度,有机会博主会讲讲css selector。
2. 由子节点定位父节点
由子节点想要定位到父节点就有点难度了,对以下代码:
<html>
<body>
<p id="A">
<!--子节点定位父节点-->
<p>
<p>child to parent
<p>
<p id="C"></p>
</p>
</p>
</p>
</p>
</body>
</html>
我们想要由 C节点 定位其两层父节点的p,示例代码如下:
# -*- coding: utf-8 -*-
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('D:\\py\\AutoTestFramework\\src\\others\\test.html')
# 1.xpath: `.`代表当前节点; '..'代表父节点
print driver.find_element_by_xpath("//p[@id='C']/../..").text
# 2.xpath轴 parent
print driver.find_element_by_xpath("//p[@id='C']/parent::*/parent::p").text
driver.quit()
结果:
child to parent
child to parent
这里我们有两种办法,第1种是..
的形式,就像我们知道的,.
表示当前节点,..
表示父节点;第2种办法跟上面一样,是xpath轴中的一个:parent
,取当前节点的父节点。这里也是css selector的一个痛点,因为css的设计不允许有能够获取父节点的办法(至少目前没有)
3. 由弟弟节点定位哥哥节点
这是第3、第4种情况,我们这里要定位的是兄弟节点了。如以下源码:
<html>
<body>
<p>
<!--下面两个节点用于兄弟节点定位-->
<p>brother 1</p>
<p id="D"></p>
<p>brother 2</p>
</p>
</body>
</html>
怎么通过 D节点 定位其哥哥节点呢?看代码示例:
# -*- coding: utf-8 -*-
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('D:\\Code\\py\\AutoTestFramework\\src\\others\\test.html')
# 1.xpath,通过父节点获取其哥哥节点
print driver.find_element_by_xpath("//p[@id='D']/../p[1]").text
# 2.xpath轴 preceding-sibling
print driver.find_element_by_xpath("//p[@id='D']/preceding-sibling::p[1]").text
driver.quit()
结果
brother 1
brother 1
这里博主也列举了两种方法,一种是通过该节点的父节点来获得哥哥节点,另外一种比较优雅,是通过xpath轴:preceding-sibling
,其能够获取当前节点的所有同级哥哥节点,注意括号里的标号,1
代表着离当前节点最近的一个哥哥节点,数字越大表示离当前节点越远,当然,xpath轴:preceding
也可以,但是使用起来比较复杂,它获取到的是该节点之前的所有非祖先节点(这里不太好解释,改天专门写篇博文讲解下所有的轴)
4. 由哥哥节点定位弟弟节点
源码与3
一致,要想通过 D节点 定位其弟弟节点,看代码示例:
# -*- coding: utf-8 -*-
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('D:\\Code\\py\\AutoTestFramework\\src\\others\\test.html')
# 1.xpath,通过父节点获取其弟弟节点
print driver.find_element_by_xpath("//p[@id='D']/../p[3]").text
# 2.xpath轴 following-sibling
print driver.find_element_by_xpath("//p[@id='D']/following-sibling::p[1]").text
# 3.xpath轴 following
print driver.find_element_by_xpath("//p[@id='D']/following::*").text
# 4.css selector +
print driver.find_element_by_css_selector('p#D + p').text
# 5.css selector ~
print driver.find_element_by_css_selector('p#D ~ p').text
driver.quit()
结果:
brother 2
brother 2
brother 2
brother 2
brother 2
博主分享了五种方法定位其弟弟节点,上面三种是用xpath,第一种很好理解,第二种用到了xpath轴:following-sibling
,跟preceding-sibling
类似,它的作用是获取当前节点的所有同级弟弟节点,同样,1
代表离当前节点最近的一个弟弟节点,数字越大表示离当前节点越远;第三种用到了xpath轴:following
,获取到该节点之后所有节点,除了祖先节点(跟preceding
方向相反,但因为往下顺序容易读,不容易出错,所以也是可以用来获取弟弟节点的,但也不建议这么使用);第四、第五种,我们用到了css selector,+
和~
的区别是:+
表示紧跟在当前节点之后的p节点,~
表示当前节点之后的p节点,如果用find_elements,则可获取到一组p节点。
为什么买:这才是最应该思考的产品差异化定位
为什么现在买?为什么买你的?为什么买?这是3个不同高度的问题。双11又要来临了,我依然很平静,多年前我写过一篇关于双11的文章,题目是:双1
vivo正式发布APEX 2020,定位Vision Beyond,持续发力“非
2020年2月28日,北京——今日,vivo正式发布了旗下第三代APEX概念机——APEX 2020。这款集结多项前沿技术的全新力作,不仅延续了前两代
很多人会市场定位,却不会定位竞争对手
最近瑞幸咖啡迅速蹿红的方式,让人不得不钦佩其优秀的营销能力。经过这一波刷屏级的品牌营销后,大家都会有意无意的拿星巴克及其他咖
手机定位软件破解版
其实是不推荐大家使用破解版软件的,因为破解版软件会被植入破解者加入的木马或广告。天下没有免费的午餐。推荐使用正版,目前
HearthBuddy炉石兄弟 格雷迈恩
getDecks(); 设置 private void getDecks() { Dictionary<string, int> tmpDeck = new Dictionary<strin
文章回顾
大家看了本文xpath —— 父子、兄弟、相邻节点定位方式详解的精彩教程资源内容,是不是对xpath —— 父子、兄弟、相邻节点定位方式详解了解更多,真心希望xpath —— 父子、兄弟、相邻节点定位方式详解能帮助到你, 小编会一直给你带来更多教程资源文章信息。
本文在撰写过程中会借鉴文案,对内容不作任何保证或承诺,请读者自行参考斟酌。网站发布的信息(包含但不限于版式、图片、字体、文章等素材)由第三方用户分享,版权归原作者所有,本站不承担任何相关的版权纠纷等相关责任。如您认为本篇内容侵犯了您的权益,请与我们联系,我们会及时处理。
本文标题:xpath —— 父子、兄弟、相邻节点定位方式详解