2

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.

Chan
  • 3,605
  • 9
  • 29
  • 60
  • 2
    Possible duplicate of [Fetch all href link using selenium in python](https://stackoverflow.com/questions/34759787/fetch-all-href-link-using-selenium-in-python) –  Feb 15 '18 at 05:08

1 Answers1

2

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')]
Alex
  • 18,484
  • 8
  • 60
  • 80