Chan
Chan

Reputation: 4291

How to find all href in an element in Selenium with Python

I have found an element and want to find all href in this element. I tried to get all the links but only the first one was obtained.

element = driver.find_element_by_xpath("//div[@class='msg']/div[@class='']")
href = element.find_element_by_css_selector('a').get_attribute('href') #only get first link 

What is the correct method to do it?

Thank you very much.

Upvotes: 1

Views: 1622

Answers (1)

Alex
Alex

Reputation: 19104

See the Selenium docs.

To find multiple elements (these methods will return a list)...

The multiple element version for css is find_elements_by_css_selector.

element = driver.find_element_by_xpath("//div[@class='msg']/div[@class='']")
hrefs = [x.get_attribute('href') for x in element.find_elements_by_css_selector('a')]

Upvotes: 2

Related Questions