20

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.

2

2 Answers 2

36

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").

Sign up to request clarification or add additional context in comments.

8 Comments

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.
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.
@Koteu, if my answer was helpful -- check a tick near my answer, please. Thanks.
@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.
I'm doing this in Feb 2019, using Python 3.7 and latest selenium: with <a></a> tag element, element.get_attribute("value") returns None, but element.text does return the text inside <a> tag....
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.