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

Time:01-12

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?

CodePudding user response:

You need to apply .text on the received web element object, as following:

text_value = driver.find_element(By.CSS_SELECTOR, css_locator).text

Of couse you can use any other approach to locate the element, not just by CSS_SELECTOR, this is just a sample code

CodePudding user response:

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
  • Related