Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Selenium: Get link text for every link on a website

I would like to fetch the linktext for a given website only and save them to a list. For example, in the following case I would like to save "Aeris Environmental" rather than "/Company/aeris-environmental"

<a href="/Company/aeris-environmental"> Aeris Environmental</a>

Using the following code I managed to retrieve the actual links but not the text.

elems = browser.find_elements(By.XPATH,'//a[@href]')
for elem in elems:
    print(elem.get_attribute("href"))

How can I achieve the same but with the text of a link?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Try this:

elems = browser.find_elements(By.XPATH,'//a')
for elem in elems:
    print(elem.text)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading