Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the value of an element in Python + Selenium?

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.

like image 274
czyngis Avatar asked Jan 07 '18 17:01

czyngis


People also ask

How do you find the value of an element in Selenium?

We can get the value of a HTML input with Selenium webdriver. This is achieved with the help of the getAttribute() method. To retrieve the value of the field with tagname input, we have to pass the value as parameter to the getAttribute() method. Let us consider an html code for a html input.

How do I get the input field value in Python?

The get_attribute() method is capable of obtaining the value we have entered in an input box. To get the value, we have to pass value as a parameter to the method. First of all, we have to identify the input box with the help of any of the locators like id, class, name, css or xpath.

How do you find the attributes of an element in Python?

get_attribute method is used to get attributes of an element, such as getting href attribute of anchor tag. This method will first try to return the value of a property with the given name. If a property with that name doesn't exist, it returns the value of the attribute with the same name.

Can I find element by class Selenium Python?

Luckily, Selenium offers a few methods you can use to find elements. One of the options the Selenium WebDriver library provides for locating HTML elements is the use of the class property. The HTML class property is used for setting a class to an HTML element.


2 Answers

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

like image 126
Ratmir Asanov Avatar answered Sep 28 '22 04:09

Ratmir Asanov


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

like image 44
undetected Selenium Avatar answered Sep 28 '22 04:09

undetected Selenium