Selenium Python how to get text (html source) from <div>
I am trying to get the text $27.5 inside the <div> , I found the element by id, and this element is called "price".
The html snippet 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 tried
price.text price.get_attribute('value') Both of the above do not work.
Update: Thanks to everyone who is trying to help. I combined your answers and got a solution :)
price = driver.find_element_by_xpath("//div[@class='price inlineBlock strong mediumText']") price_content = price.get_attribute('innerHTML') print price_content.strip() The element is hidden, the last time I worked with Selenium , you could not get the text of the hidden elements. However, you can always execute javascript, I usually don't write in python, but it should be something like:
def val = driver.execute_script("return document.getElementById('locator').innerHTML") I tried your edited solution, but they only get 1 div having a class . So, I tried this below to print a list of div with the same class .
Replacing element with elements will result in a list:
price = driver.find_elements_by_xpath('//div[@class = "price inlineBlock strong mediumText"]') Use for... in range() to print the list:
num = len (price) for i in range (num): print (price[i].text)