Home > database >  Selenium: Get link text for every link on a website
Selenium: Get link text for every link on a website

Time:12-08

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?

CodePudding user response:

Try this:

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