当前位置:首页 >> 脚本专栏

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()
driver.get(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:\Python36\python.exe E:/python/test1/day1/test9.py
html


Process finished with exit code 0

(2)//,从目标节点下选取

下面的xpath,意思就是我要去找head,找到就保存到eles里面,不一定需要在根目录下面找

from selenium import webdriver

driver = webdriver.Chrome()
driver.get(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:\Python36\python.exe E:/python/test1/day1/test9.py
head


Process finished with exit code 0

如果我换成下面的xpath,结果会如何呢?

from selenium import webdriver

driver = webdriver.Chrome()
driver.get(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()
driver.get(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:\Python36\python.exe 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()
driver.get(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:\Python36\python.exe E:/python/test1/day1/test9.py
meta


Process finished with exit code 0

3、谓语实验

(1)[1]

选择第一个form元素下面的第一个input元素,打印name属性值

eles1 = driver.find_elements_by_xpath('//form[1]/input[1]')
for ele in eles1:
 print(ele.get_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:
 print(ele.get_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 节点位置的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。