Search code examples
pythonseleniumselenium-webdriverwebdriver

Is there a selenium function for getting text on WebElements?(Python)


I’m using selenium(Python) with a chrome driver. I got a WebElement from a hotel site and I want to get it as a string. I used get_text but I get:

AttributeError: 'WebElement' object has no attribute 'getText'

How do I get text from a WebElement?


Solution

  • I've always used the textContent or innerText attribute to get the text.

    text = driver.find_element(By.CSS_SELECTOR, css_selector).textContent
    OR
    text = driver.find_element(By.CSS_SELECTOR, css_selector).innerText