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() 
+6
source share
5 answers

Can't you use regex or Beautiful Soup to find the contents of an element in HTML:

 re.search(r'<div.*?>(*.?)</div>', price.get_attribute('innerHTML')).group(1) 
+2
source

Change css selector to

 div[id$='_price'] 

Full code

  price = fltright.find_element(By.CSS_SELECTOR, "div[id$='_price']") price.text 
0
source

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") 
0
source

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) 
0
source

browser.find_element_by_xpath ("// form [@ id = 'workQueueTaskListForm'] / case [1] / p"). Text

-2
source

Source: https://habr.com/ru/post/985710/


All Articles