1

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.

1

1 Answer 1

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')]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.