简体   繁体   中英

How to get all href values of <a> in selenium python

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'

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

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")))])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM