0

I'm trying to execute this:

driver.get(websitelink)
sleep(3)

element = driver.find_element_by_id("canvasCaption").text
print('text is:',element)

In order to get text from here :

<div class="a-column a-span12 a-text-center"> 
  <span id="canvasCaption" class="a-color-secondary">Haz clic para obtener una vista ampliada</span> 
</div>

The output is only :

text is: 

But it should be:

text is:  Haz clic para obtener una vista ampliada

Also gived a try with:

element = driver.find_element_by_class_name("a-color-secondary").text

But same output.

Can anyone help please ?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
madacmjtr
  • 13
  • 6
  • Does this answer your question? [Use Python Selenium to get span text](https://stackoverflow.com/questions/14590341/use-python-selenium-to-get-span-text) – FLAK-ZOSO Jan 20 '22 at 19:59

1 Answers1

0

To print the text Haz clic para obtener una vista ampliada you can use either of the following Locator Strategies:

  • Using css_selector and get_attribute("innerHTML"):

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

    print(driver.find_element(By.XPATH, "//span[@id='canvasCaption']").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:

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

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[@id='canvasCaption']"))).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