Abdelrhman Kaamal
Abdelrhman Kaamal

Reputation: 9

How to extract the link within the href attribute from the <a> element using Selenium python

How to get link with Selenium Python.

HTML sample:

<a herf="link" ...>

How to get this link in a variable. I used many ways but didn't work. I used function find_element() example:

ele=bro.find_elements(By.TAG_NAME,'a')

Upvotes: 0

Views: 692

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193058

To extract the value of any of the attributes of a WebElement, you have to use the get_attribute() method from selenium.webdriver.remote.webelement module.


This usecase

To extract the value of the href attribute from WebElement(s) you can use the following line of code:

  • From a single element:

    print(bro.find_elements(By.TAG_NAME,'a').get_attribute("href"))
    
  • From a list of elements:

    print([my_elem.get_attribute("href") for my_elem in driver.find_elements(By.TAG_NAME,'a')])
    

Upvotes: 0

Related Questions