python定位xpath节点位置的⽅法chrome 右键有copy xpath地址
但是有些时候获取的可能不对
可以⾃⼰⽤代码验证⼀下
如果还是不⾏可以考虑从源码当中取出来
趁热打铁,使⽤前⼀篇⽂章中 XPath 节点来定位HTML 页⾯。
HTML⽂件如下(您可以将其拷贝,保存成html⽂件,跟我笔者实验):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Storm</title>
</head>
<body>
<h1 id="h1" name="hname" class="cname">这是⼀个h1标签</h1>
<form>
⽂本域1:<input type="text" name="first_name">
<br>
⽂本域2:<input type="text" name="last_name">
</form>
<form>
密码字段:<input type="password" name="password">
</form>
<form>
单选按钮1:
<input type="radio" name="radio1" value="nan">male
<input type="radio" name="radio1" value="nv">female
</form>
<form>
宠物:
<input type="checkbox" name="cw">猫
<input type="checkbox" name="cw">狗
<input type="checkbox" name="cw">兔⼦
</form>
</body>
</html>
1、节点
上⾯的HTML⽂件,<html> 为根节点,他有个lang的属性,他有两个⼦节点<head>和<body>。2、选取节点实验
(1)/,从根节点选取
下⾯的代码从根节点开始选取所有的html元素(这⾥只有⼀个),打印tag name,就是html
from selenium import webdriver
driver = webdriver.Chrome()
<(r'file:///E:\python\test1\day1\test0.html')
eles = driver.find_elements_by_xpath('/html')
for ele in eles:
print(ele.tag_name)
driver.quit()
运⾏结果
C:\ E:/python/test1/day1/test9.py
html
Process finished with exit code 0
(2)//,从⽬标节点下选取
下⾯的xpath,意思就是我要去head,到就保存到eles⾥⾯,不⼀定需要在根⽬录下⾯
from selenium import webdriver
driver = webdriver.Chrome()
<(r'file:///E:\python\test1\day1\test0.html')
eles = driver.find_elements_by_xpath('//head')
for ele in eles:
print(ele.tag_name)
driver.quit()
运⾏结果:
C:\ E:/python/test1/day1/test9.py
head
Process finished with exit code 0
如果我换成下⾯的xpath,结果会如何呢?
from selenium import webdriver
driver = webdriver.Chrome()
<(r'file:///E:\python\test1\day1\test0.html')
eles = driver.find_elements_by_xpath('/head')
for ele in eles:
print(ele.tag_name)
driver.quit()
从根节点选取head元素,跟节点不是head元素,所以不到,打印为空
(3). ,选取当前节点;.. ,选取⽗节点
下⾯的xpath,第⼀个,匹配到head元素,然后分配head当前节点(就是head);head⽗节点(是html)from selenium import webdriver
driver = webdriver.Chrome()
<(r'file:///E:\python\test1\day1\test0.html')
eles2 = driver.find_elements_by_xpath('//head/.')
eles3 = driver.find_elements_by_xpath('//head/..')
for ele in eles2:
print(ele.tag_name)
for ele in eles3:
print(ele.tag_name)
driver.quit()
运⾏结果:
C:\ E:/python/test1/day1/test9.py
head
html
Process finished with exit code 0
(4)@ 选取属性
下⾯xpath为,匹配任意元素,其有个属性charset,值为UTF-8。
from selenium import webdriver
driver = webdriver.Chrome()
<(r'file:///E:\python\test1\day1\test0.html')
eles3 = driver.find_elements_by_xpath('//*[@charset="UTF-8"]')
for ele in eles3:
print(ele.tag_name)
driver.quit()
运⾏结果为:
C:\ E:/python/test1/day1/test9.py
meta
Process finished with exit code 0xpath语法 python
3、谓语实验
(1)[1]
选择第⼀个form元素下⾯的第⼀个input元素,打印name属性值eles1 = driver.find_elements_by_xpath('//form[1]/input[1]')
for ele in eles1:
_attribute('name'))
运⾏结果:first_name
(2)[last()]
eles1 = driver.find_elements_by_xpath('//form[1]/input[last()]')
运⾏结果:last_name
(3)[last()-1]
eles1 = driver.find_elements_by_xpath('//form[1]/input[last()-1]')
运⾏结果:first_name
(4)[position()<3]
eles1 = driver.find_elements_by_xpath('//form[1]/input[position()<3]')
运⾏结果:
first_name
last_name
(5)h1[@class]
在body元素下层具有class属性的h1标签
eles1 = driver.find_elements_by_xpath('//body/h1[@class]')
for ele in eles1:
print(ele.tag_name)
(6)h1[@class="cname"]
在body元素下层具有class属性的h1标签,且值为cname eles1 = driver.find_elements_by_xpath('//body/h1[@class="cname"]')(7)input[xxx>35]
这个没到合适的例⼦,暂缺
4、选取未知节点——通过通配符实现
(1)//form[1]/*
选择form[1]下的所有元素
eles1 = driver.find_elements_by_xpath('//form[1]/*')
for ele in eles1:
_attribute('name'))
运⾏结果:
first_name
None
last_name
(2)//*
选择所有元素
eles1 = driver.find_elements_by_xpath('//*')
for ele in eles1:
print(ele.tag_name)
运⾏结果:
html
head
meta
title
body
h1
form
input
br
input
form
input
form
input
input
form
input
input
input
input
(3)//input[@*]
匹配只要有任意属性的input元素
以上这篇python定位xpath 节点位置的⽅法就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论