0

Hi I am using selenium I want to find all values of hrefs present in webelement

links=profiles.find_elements_by_tag_name("a").get_attribute('href')

But it gives me error

AttributeError: 'list' object has no attribute 'get_attribute'
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

0

You are getting list of a tags so you can loop around and get your informations

a_list = profiles.find_elements_by_tag_name("a")
all_links = [item.get_attribute('href') for item in a_list]

all_links will have list of urls you required

Akash senta
  • 483
  • 7
  • 16
0

find_elements_by_tag_name(name) would return a list. Where as get_attribute(name) gets the given attribute or property of the WebElement. Hence the error:

AttributeError: 'list' object has no attribute 'get_attribute'

Solution

You need to iterate through the elements and extract the value of the href attributes as follows:

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

Ideally, you need to induce WebDriverWait for the visibility_of_all_elements_located() and you can use either of the following Locator Strategies:

print([my_elem.get_attribute("href") for my_elem in WebDriverWait(profiles, 20).until(EC.visibility_of_all_elements_located((By.TAG_NAME, "a")))])
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352