0

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]") 
1
  • I think you're on the wrong track here : href is not a DOM element ; it is a property of the a element. See the documetation for practical examples. Commented Apr 4, 2017 at 7:43

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.