19

I have got this HTML element in my Python (3.6.3) code (as a Selenium webelement of course):

<span class="ocenaCzastkowa masterTooltip" style="color:#000000;" alt="Kod:
pd1<br/>Opis: praca domowa<br/>Waga: 2,00<br/>Data: 12.09.2017<br/>Nauczyciel:
(NAME CENSORED)">5</span>

And I want to get the value at the end (which is 5 in this case) and I have got no idea how to get it.

Obviously, I can't use webelement.get_attribute() because I don't know the name of the attribute.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
czyngis
  • 413
  • 2
  • 5
  • 18
  • 2
    Possible duplicate of [How to get text with selenium web driver in python](https://stackoverflow.com/questions/20996392/how-to-get-text-with-selenium-web-driver-in-python) – Andersson Jan 07 '18 at 18:46
  • [Two nearly](https://stackoverflow.com/questions/20996392/how-to-get-text-with-selenium-webdriver-in-python/65863434#65863434) ***identical answers*** [suggest](https://stackoverflow.com/questions/48139676/how-to-get-the-value-of-an-element-in-python-selenium/65861880#65861880) it is a duplicate. – Peter Mortensen Jun 26 '22 at 11:02

2 Answers2

34

Try the following code:

span_element = driver.find_element_by_css_selector(".ocenaCzastkowa.masterTooltip")
span_element.text # This will return "5".

PS: You can also use span_element.get_attribute("value").

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
  • I am confused. Before, i used find_elements_by_xpath(many elements, and this worked, all the webelements were there) elementlist[x].getattribute('value') did not work. Although now, when i used find_element_by_css_selector(css_selector).get_attribute('value') it returns the desired result. Thanks. – czyngis Jan 07 '18 at 17:33
  • The error :`selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document.` I tried doing some research on this error but did not get far. – czyngis Jan 07 '18 at 17:35
  • @Koteu, if my answer was helpful -- check a tick near my answer, please. Thanks. – Ratmir Asanov Jan 07 '18 at 17:36
  • @Ratmir Yeah i know, i'll wait a while, perhaps somebody figures out the problem. If there will be no suggestions i will accept your anwser. By the way, Thank You for the instant anwser. – czyngis Jan 07 '18 at 17:38
  • @Koteu, `.text` also must work. Maybe, needed to wait (implicit or explicit). You didn't push your code. – Ratmir Asanov Jan 07 '18 at 17:43
  • Somehow or other, now trying it again in idle, again creating the list in the very same way as before, the error does not appear anymore (when refering to the element from the list). Thank You. Still, any clarification on the error wich appeared would be appreciated – czyngis Jan 07 '18 at 17:48
  • 1
    I'm doing this in Feb 2019, using Python 3.7 and latest selenium: with `` tag element, `element.get_attribute("value")` returns `None`, but `element.text` does return the text inside `` tag.... – Will Croxford Feb 19 '19 at 15:48
  • @WillCroxford, it depends on the realization of your site. Maybe, you need to use there explicit wait before interacting with the element. – Ratmir Asanov Feb 19 '19 at 16:04
2

To print the textContent, i.e. 5, you can use either of the following Locator Strategies:

  • Using css_selector:

    print(driver.find_element(By.CSS_SELECTOR, "div.ocenaCzastkowa.masterTooltip").text)
    
  • Using XPath:

    print(driver.find_element(By.XPATH, "//span[@class='ocenaCzastkowa masterTooltip']").text)
    

Ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.ocenaCzastkowa.masterTooltip"))).text)
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[@class='ocenaCzastkowa masterTooltip']"))).text)
    
  • Note: You have to add the following imports:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352