Muthu
Muthu

Reputation: 33

How to locate the href element using selenium python

How to locate the href elements using selenium and python

i tried below code but its not working

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

Upvotes: 0

Views: 1196

Answers (2)

Akarsh
Akarsh

Reputation: 967

Try below code with replacing href attribute value with your value.

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

Upvotes: 0

Andersson
Andersson

Reputation: 52685

To locate href attribute usually you can use

//a/@href

but selenium doesn't support this syntax as in selenium you can locate webelements only.

You can try below:

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

With this line of code you should be able to get href attribute of the first anchor element on the page.

Upvotes: 2

Related Questions