selenium如何在特定目标类中获取href的内容

我正在尝试从下面的html中检索网页中的数据

       <div class="someclass">

<p class="name"><a href="#/word/1/">helloworld</a></p>

</div>

我的目标是解析“#/ word / 1 /”

        target = self.driver.find_element_by_class_name('someclass')

print target

print target.text

print target.get_attribute("css=a@href")

print target.tag_name

但输出是

 <selenium.webdriver.remote.webelement.WebElement object at 0x10bf16210>

helloworld

None

div

我尝试了很多方法,似乎无法在目标类中获取“ a href”的内容。

我真的不想做的是获取页面的源代码,然后进行字符串搜索,这似乎很愚蠢。

反正得到那个?

回答:

据我所知,您可以通过搜索子元素来获取href

div = self.driver.find_element_by_class_name('someclass')

div.find_element_by_css_selector('a').get_attribute('href')

以上是 selenium如何在特定目标类中获取href的内容 的全部内容, 来源链接: utcz.com/qa/429623.html

回到顶部