简体   繁体   English

如何使用Python在Selenium中的元素中查找所有href

[英]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. 我已经找到一个元素,并希望在此元素中找到所有href。 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 . 请参阅Selenium文档

To find multiple elements (these methods will return a list)... 查找多个元素(这些方法将返回一个列表)...

The multiple element version for css is find_elements_by_css_selector . CSS的多元素版本为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')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM