152

I'm trying to get text using Selenium WebDriver and here is my code. Please note that I don't want to use XPath, because in my case the ID gets changed on every relaunch of the web page.

My code:

text = driver.find_element_by_class_name("current-stage").getText("my text")

HTML:

<span class="current-text" id="yui_3_7_0_4_1389185744113_384">my text</span>

How can I fix this?

MattDMo
  • 100,794
  • 21
  • 241
  • 231
user3121891
  • 1,807
  • 3
  • 15
  • 13
  • 1
    If done interactively, the result may be something like the error message *"AttributeError: 'current-stage' object has no attribute 'getText'"* – Peter Mortensen Nov 22 '20 at 06:11

9 Answers9

264

You want just .text.

You can then verify it after you've got it, don't attempt to pass in what you expect it should have.

Arran
  • 24,648
  • 6
  • 68
  • 78
  • Im getting this error - 'WebDriver' object has no attribute 'getText' – user3121891 Jan 08 '14 at 13:07
  • 3
    @user3121891, it's `.text`. – Arran Jan 08 '14 at 13:09
  • Now Im using this code: driver.find_element_by_class_name("current-stage").text("my text") if text: print "pass" else: print "Fail" and getting this error - 'unicode' object is not callable – user3121891 Jan 08 '14 at 13:18
  • 18
    @user3121891 `driver.find_element_by_class_name("current-stage").text` ....just **text** on it's own. *Nothing* else. – Arran Jan 08 '14 at 13:18
  • how to get .text with list of selenium object? I am getting error list object dose not have text method – Ami Patel Sep 27 '16 at 14:04
  • 1
    You are going to want to loop through the list of selenium objects and apply the `.text` on each item of the for loop. – pppp Nov 08 '18 at 16:44
  • My text is getting truncated, showing ellipsis (...) instead of complete the text, any idea how to solve that? – GitHunter0 May 03 '21 at 04:10
  • @GitHunter0 likely dependant on your app - is it truncated on the actual page? – Arran May 04 '21 at 12:45
  • 1
    Yes @Arran, when I inspect the element it shows the complete text but when I try to scrape it, I can only retrieve the abbreviated version which is the one actually displayed in the website (the designers of the site did that because some text can be too long for the intended style) – GitHunter0 May 05 '21 at 00:17
  • When I run this code, it runs without error, but then when I print it I get NULL `abstract = driver.find_element_by_xpath("//h5[contains(text(),'Abstract')]").text` – aayush_malik Oct 22 '21 at 02:15
  • The gist may be that [in Java](https://stackoverflow.com/questions/20996392/how-to-get-text-with-selenium-webdriver-in-python/45863411#45863411) it is `getText`, not `text` (perhaps Java was the original *or* most commonly used "driver" language?). In particular, some examples may be in Java. For instance, the default view [on www.selenium.dev](https://www.selenium.dev/documentation/webdriver/elements/finders/) is for Java (the others are Python, C#, Ruby, JavaScript, and Kotlin—in that order). – Peter Mortensen Mar 25 '22 at 13:48
112

Python

element.text

Java

element.getText()

C#

element.Text

Ruby

element.text
Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
  • 3
    I can't find any documentation on this for Python, where are you guys finding this? – Amon Oct 22 '18 at 20:00
  • 2
    @CharlesSmith if you put a `.` after the element in VSCode, you'll be given a list of suggestions based on what the class accepts. That's how I found this. – CodeSpent Dec 21 '18 at 00:37
  • 1
    Yes I noticed the same in IntelliJ, was just wondering why it's not in the docs – Amon Dec 21 '18 at 14:50
  • 2
    @Charles Smith: It is on *[Web element](https://www.selenium.dev/documentation/en/webdriver/web_element/)*, sub section *"Get Element Text"* (at the bottom of the page). There are six different languages and the default is for JavaScript. Click on *"Python"* to see the Python code example. – Peter Mortensen Nov 22 '20 at 06:16
  • (User "Charles Smith" seems to have changed name to "Amon".) – Peter Mortensen Mar 25 '22 at 13:54
  • That link is half-broken by now. An ***example*** for use of `getText` / `text` is now on a sub page to *"Web elements"* — *[Finding web elements](https://www.selenium.dev/documentation/webdriver/elements/finders/)*, sub section *"Get element"*. Note also that clicking a tab (e.g., "Python") changes all the views on all pages, not just the one clicked on. That is an example where `getText` / `text` happens to be used. I don't know where the formal documentation for `getText` / `text` is. It must be somewhere on that site. – Peter Mortensen Mar 25 '22 at 14:10
  • What was the point of adding "\*" to "Ruby"? What does it mean? – Peter Mortensen Nov 11 '22 at 14:11
21

To print the text my text you can use either of the following Locator Strategies:

  • Using class_name and get_attribute("textContent"):

    print(driver.find_element(By.CLASS_NAME, "current-stage").get_attribute("textContent"))
    
  • Using css_selector and get_attribute("innerHTML"):

    print(driver.find_element(By.CSS_SELECTOR, "span.current-stage").get_attribute("innerHTML"))
    
  • Using xpath and text attribute:

    print(driver.find_element(By.XPATH, "//span[@class='current-stage']").text)
    

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

  • Using CLASS_NAME and get_attribute("textContent"):

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CLASS_NAME, "current-stage"))).get_attribute("textContent"))
    
  • Using CSS_SELECTOR and text attribute:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "span.current-stage"))).text)
    
  • Using XPATH and get_attribute("innerHTML"):

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[@class='current-stage']"))).get_attribute("innerHTML"))
    
  • 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


References

Link to useful documentation:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
11

The answer is:

driver.find_element_by_class_name("ctsymbol").text
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rearThing
  • 632
  • 3
  • 9
  • 26
7

You can use:

element = driver.find_element_by_class_name("class_name").text

This will return the text within the element and will allow you to verify it after that.

oroy
  • 82
  • 16
3

This is the correct answer. It worked!!

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Chrome("E:\\Python\\selenium\\webdriver\\chromedriver.exe")
driver.get("https://www.tatacliq.com/global-desi-navy-embroidered-kurta/p-mp000000000876745")
driver.set_page_load_timeout(45)
driver.maximize_window()
driver.implicitly_wait(2)
driver.get_screenshot_as_file("E:\\Python\\Tatacliq.png")
print ("Executed Successfully")
driver.find_element_by_xpath("//div[@class='pdp-promo-title pdp-title']").click()
SpecialPrice = driver.find_element_by_xpath("//div[@class='pdp-promo-title pdp-title']").text
print(SpecialPrice)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
subhasis
  • 31
  • 1
1

A heads up for anyone finding this thread after the Selenium 4 update. The driver.find_element_by_* has been deprecated and using it will give a "deprecationwarning". The replacement method is: driver.find_element(By.X,"name") Please look up Selenium 4 info.

  • Yes. E.g., from [a comment](https://stackoverflow.com/questions/30002313/selenium-finding-elements-by-class-name-in-python#comment128785684_30025430): *"`find_element_by_*` and `find_elements_by_*` are removed in Selenium 4.3.0. Use `find_element` instead."*. Though it doesn't really answer the question what can be done if the number of elements is different from exactly one (e.g., thrown exceptions if an element is absent. The only solution may be to [catch the exception](https://stackoverflow.com/a/12150013) (no way to prevent it)). – Peter Mortensen Nov 11 '22 at 14:07
0

I've found this absolutely invaluable when unable to grab something in a custom class or changing id's:

driver.find_element_by_xpath("//*[contains(text(), 'Show Next Date Available')]").click()
driver.find_element_by_xpath("//*[contains(text(), 'Show Next Date Available')]").text
driver.find_element_by_xpath("//*[contains(text(), 'Available')]").text
driver.find_element_by_xpath("//*[contains(text(), 'Avail')]").text
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

to get a text from the element:

url=driv.find_element(By.whatDoYouWant, "ClassNameOrwhatDoYouWant").text
print(url)

ex:

url = web.find_element(By.TAG_NAME, "a").text
print(url)

to get a text from the elements:

ex:

for i in range(6):
    var = web.find_elements(By.TAG_NAME, "input")[i].text
    print(var)
z3rone
  • 176
  • 14
Abdullah
  • 1
  • 1
  • 1
    I'd suggest using the formatting options available in the editor and mark source code properly, so that your answer is more readable and easier to understand. – xjmdoo Feb 16 '23 at 17:51