1. 程式人生 > 程式設計 >python定位xpath 節點位置的方法

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 節點位置的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。