Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
397 views
in Technique[技术] by (71.8m points)

python - selenium how to get the content of href within some targeted class

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...