python用selenium獲取元素標簽內容和屬性值


舉個栗子

百度搜索右上角有個“百度首頁”的小標簽鏈接

html:

<a class="toindex" href="/">百度首頁</a>

xpath為:

//*[@id="u"]/a[1]

獲取標簽內容,也就是“百度首頁“”這幾個字

python:

a=driver.find_element_by_xpath('//*[@id="u"]/a[1]')
print(a.text)

輸出:

百度首頁

 

獲取class屬性內容(其他屬性同理,把class替換成屬性名稱就可以)

a=driver.find_element_by_xpath('//*[@id="u"]/a[1]')
print(a.get_attribute('class'))

輸出:

toindex

selenium的啟動和運行見其他文章


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM