Reputation: 4291
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
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