硒如何获得某些目标类内的href内容 [英] selenium how to get the content of href within some targeted class

查看:113
本文介绍了硒如何获得某些目标类内的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.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')


I am trying to retrieve the data from the webpage has the html in below

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

My goal is to parse "#/word/1/" What I did is

        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

but the output are

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

I tried so many ways , it seems there is no way i can get the content of 'a href' within the targeted class.

I really dont want to do is get the source code of the page, and then do a string searching, that seems dumb....

anyway to get that?

解决方案

As far as I am aware you can get the href by searching through the child elements

div = self.driver.find_element_by_class_name('someclass')
div.find_element_by_css_selector('a').get_attribute('href')

这篇关于硒如何获得某些目标类内的href内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆