2017-04-04 303 views
0

如何查找使用硒和python如何使用硒蟒蛇

我嘗試下面的代碼,但在href要素來查找HREF元素它不工作

driver.find_element_by_xpath("//a[@href]") 
+0

我覺得你在錯誤的軌道上:'href'是_not_一個DOM元素;它是'a'元素的一個屬性。有關實際示例,請參見[documetation](http://selenium-python.readthedocs.io/locating-elements.html#locating-by-xpath)。 –

回答

1

要找到href屬性平時也可以使用

//a/@href 

selenium不支持此語法,如selenium您只能找到webelements。

你可以試試下面:

driver.find_element_by_xpath("//a").get_attribute('href') 

有了這行代碼,你應該能夠得到第一錨件href屬性頁面上。

0

用下面的代碼嘗試用你的值替換href屬性值。

driver.find_element_by_xpath("//a[@href='href attribute value']");