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 ?

1

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.