Python selenium如何在某个目标类中获取href的内容
我正在尝试从网页中检索数据,该网页包含下面的htmlPython selenium如何在某个目标类中获取href的内容,python,html,css,xpath,selenium,Python,Html,Css,Xpath,Selenium,我正在尝试从网页中检索数据,该网页包含下面的html <div class="someclass"> <p class="name"><a href="#/word/1/">helloworld</a></p> </div> 但产出是有限的 <selenium.webdriver.remote.webelement.WebElement object at 0x10bf16
<div class="someclass">
<p class="name"><a href="#/word/1/">helloworld</a></p>
</div>
但产出是有限的
<selenium.webdriver.remote.webelement.WebElement object at 0x10bf16210>
helloworld
None
div
地狱世界
没有一个
div
我尝试了很多方法,似乎我无法在目标类中获取“a href”的内容
我真的不想做的是获取页面的源代码,然后进行字符串搜索,这似乎很愚蠢
是否要获取该文件?据我所知,您可以通过搜索子元素来获取href
div = self.driver.find_element_by_class_name('someclass')
div.find_element_by_css_selector('a').get_attribute('href')
这应该可以为您做到:
self.driver.find_element_by_css_selector('.someclass a').get_attribute('href')
如果您从find\u element\u按\u id或classname或xpath搜索特殊标记用法 然后使用获取属性('href') 在本例中,打印标记的所有属性
ids = self.driver.find_elements_by_xpath('//*[@href]')
for id in ids:
print(id.get_attribute('href'))
为什么没有得到实际的锚元素
anchoreElement=target。通过标记名称('a')查找元素。
…然后您可以执行print anchoreElement.get_attribute(“href”)
…对吧…?如果没有你,我自己永远也不会想到这一点。非常感谢!
ids = self.driver.find_elements_by_xpath('//*[@href]')
for id in ids:
print(id.get_attribute('href'))