Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 640
runcode Avatar asked Oct 29 '13 16:10

runcode


People also ask

How do you get the content inside a tag in Selenium?

The Selenium WebDriver interface has predefined the getText() method, which helps retrieve the text for a specific web element. This method gets the visible, inner text (which is not hidden by CSS) of the web-element.

How do I click on a specific link in Selenium?

New Selenium IDE A hyperlink on a page is identified with the anchor tag. To click a link, we can use the link text locator which matches the text enclosed within the anchor tag. We can also use the partial link text locator which matches the text enclosed within the anchor tag partially.

How do you find the href of an element?

Use the querySelector() method to get an element by an href attribute, e.g. document. querySelector('a[href="https://example.com"]') . The method returns the first element that matches the selector or null if no element with the provided selector exists in the DOM.


2 Answers

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') 
like image 123
aychedee Avatar answered Sep 22 '22 08:09

aychedee


This should do it for you:

self.driver.find_element_by_css_selector('.someclass a').get_attribute('href') 
like image 43
Andrew Avatar answered Sep 19 '22 08:09

Andrew