简体   繁体   中英

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.

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

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