1

How do I get the TEXT of a hyperlink with selenium and python?

This is the html in question:

<a ng-href="/example/example2/" href="/example/example2/">NEEDED TEXT</a>

I already have the element of the 'a' tag and I am able to successfuly get the link by doing element.get_attribute("href").

But when I try to do element.text i am not able to get "NEEDED TEXT". What is the reason for this?

2
  • What is exactly happening when you invoke element.text?
    – Alexey R.
    Dec 26, 2018 at 10:12
  • 1
    It just returns an empty string ' ' when i do element.text . I ended up getting it to work with both element.get_attribute('innerHTML') and element.get_attribute('text')
    – E. Jin
    Jan 3, 2019 at 7:01

2 Answers 2

2

Are you sure that there are no other elements within the <a> tag?
element.text should return the text.
You can also try element.get_attribute('innerHTML') and check whether it works.

2

Ideally the element.text should work. If it doesn't, then you can try any of element.get_attribute('innerHTML') or element.get_attribute('text'). Note that the parameters to get_attribute is case sensitive.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.