Skip to content
Advertisement

Getting text from a object with Selenium

I’m trying to get the text inside of <pre> tag and I have tried with get_attribute('text'), get_attribute('value'), .text(), .value(), get_attribute("innerHTML") but I keep failing:

Snapshot:

enter image description here

This is the code that i’m using:

JavaScript

And this is what it says when print: enter image description here

Advertisement

Answer

To print the text within the <pre> tag you can use either of the following locator strategies:

  • Using css_selector and get_attribute("innerHTML"):

    JavaScript
  • Using xpath and text attribute:

    JavaScript

To extract the 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 and text attribute:

    JavaScript
  • Using XPATH and get_attribute("innerHTML"):

    JavaScript
  • Note : You have to add the following imports :

    JavaScript

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium – Python

Advertisement