您好,登錄后才能下訂單哦!
chrome 右鍵有copy xpath地址
但是有些時(shí)候獲取的可能不對(duì)
可以自己用代碼驗(yàn)證一下
如果還是不行 可以考慮從源碼當(dāng)中取出來(lái)
趁熱打鐵,使用前一篇文章中 XPath 節(jié)點(diǎn)來(lái)定位HTML 頁(yè)面。
HTML文件如下(您可以將其拷貝,保存成html文件,跟我筆者實(shí)驗(yàn)):
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Storm</title> </head> <body> <h2 id="h2" name="hname" class="cname">這是一個(gè)h2標(biāo)簽</h2> <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、節(jié)點(diǎn)
上面的HTML文件,<html> 為根節(jié)點(diǎn),他有個(gè)lang的屬性,他有兩個(gè)子節(jié)點(diǎn)<head>和<body>。
2、選取節(jié)點(diǎn)實(shí)驗(yàn)
(1)/,從根節(jié)點(diǎn)選取
下面的代碼從根節(jié)點(diǎn)開(kāi)始選取所有的html元素(這里只有一個(gè)),打印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()
運(yùn)行結(jié)果
C:\Python36\python.exe E:/python/test1/day1/test9.py html Process finished with exit code 0
(2)//,從目標(biāo)節(jié)點(diǎn)下選取
下面的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()
運(yùn)行結(jié)果:
C:\Python36\python.exe E:/python/test1/day1/test9.py head Process finished with exit code 0
如果我換成下面的xpath,結(jié)果會(huì)如何呢?
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()
從根節(jié)點(diǎn)選取head元素,跟節(jié)點(diǎn)不是head元素,所以找不到,打印為空
(3). ,選取當(dāng)前節(jié)點(diǎn);.. ,選取父節(jié)點(diǎn)
下面的xpath,第一個(gè),匹配到head元素,然后分配找head當(dāng)前節(jié)點(diǎn)(就是head);head父節(jié)點(diǎn)(是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()
運(yùn)行結(jié)果:
C:\Python36\python.exe E:/python/test1/day1/test9.py head html Process finished with exit code 0
(4)@ 選取屬性
下面xpath為,匹配任意元素,其有個(gè)屬性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()
運(yùn)行結(jié)果為:
C:\Python36\python.exe E:/python/test1/day1/test9.py meta Process finished with exit code 0
3、謂語(yǔ)實(shí)驗(yàn)
(1)[1]
選擇第一個(gè)form元素下面的第一個(gè)input元素,打印name屬性值
eles1 = driver.find_elements_by_xpath('//form[1]/input[1]') for ele in eles1: print(ele.get_attribute('name'))
運(yùn)行結(jié)果:first_name
(2)[last()]
eles1 = driver.find_elements_by_xpath('//form[1]/input[last()]')
運(yùn)行結(jié)果:last_name
(3)[last()-1]
eles1 = driver.find_elements_by_xpath('//form[1]/input[last()-1]')
運(yùn)行結(jié)果:first_name
(4)[position()<3]
eles1 = driver.find_elements_by_xpath('//form[1]/input[position()<3]')
運(yùn)行結(jié)果:
first_name last_name
(5)h2[@class]
在body元素下層找具有class屬性的h2標(biāo)簽
eles1 = driver.find_elements_by_xpath('//body/h2[@class]') for ele in eles1: print(ele.tag_name)
(6)h2[@class="cname"]
在body元素下層找具有class屬性的h2標(biāo)簽,且值為cname
eles1 = driver.find_elements_by_xpath('//body/h2[@class="cname"]')
(7)input[xxx>35]
這個(gè)沒(méi)找到合適的例子,暫缺
4、選取未知節(jié)點(diǎn)——通過(guò)通配符實(shí)現(xiàn)
(1)//form[1]/*
選擇form[1]下的所有元素
eles1 = driver.find_elements_by_xpath('//form[1]/*') for ele in eles1: print(ele.get_attribute('name'))
運(yùn)行結(jié)果:
first_name None last_name
(2)//*
選擇所有元素
eles1 = driver.find_elements_by_xpath('//*') for ele in eles1: print(ele.tag_name)
運(yùn)行結(jié)果:
html head meta title body h2 form input br input form input form input input form input input input input
(3)//input[@*]
匹配只要有任意屬性的input元素
以上這篇python定位xpath 節(jié)點(diǎn)位置的方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。