舉個栗子
百度搜索右上角有個“百度首頁”的小標簽鏈接
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的啟動和運行見其他文章