Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Python how to get text(html source) from <div>

I'm trying to get text $27.5 inside tag <div>, I located the element by id and the element is called "price".

The snippet of html is as follows:

<div id="PPP,BOSSST,NYCPAS,2015-04-26T01:00:00-04:00,2015-04-26T05:20:00-04:00,_price" class="price inlineBlock strong mediumText">$27.50</div>

Here is what I've tried

price.text
price.get_attribute('value')

Both of the above doesn't work.

Update: Thanks for everyone that tries to help. I combined your answers together and got the solution:)

    price = driver.find_element_by_xpath("//div[@class='price inlineBlock strong mediumText']")
    price_content = price.get_attribute('innerHTML')
    print price_content.strip()
like image 208
Kennard Avatar asked Apr 22 '15 20:04

Kennard


People also ask

How do you getText from 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.


1 Answers

Can't you use a regular expression or Beautiful Soup to find the contents of the element in HTML:

re.search(r'<div.*?>(*.?)</div>', price.get_attribute('innerHTML')).group(1)
like image 165
Malik Brahimi Avatar answered Sep 28 '22 16:09

Malik Brahimi