Home > Mobile >  How to check if text is visible on web page
How to check if text is visible on web page

Time:08-01

How does one check if this text (see picture) is visible to the user on the web page?

.is_displayed() clearly always returns true. Any alternatives?

The following always returns true (even when the text is not visible to the user)

driver.find_element(by=By.CSS_SELECTOR, value=".has-primary-color.has-text-color").is_displayed()

enter image description here

CodePudding user response:

To probe if the text within <h3> tag is visible to the user on the web page or not you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "h3.has-primary-color.has-text-color")))
    
  • Using XPATH:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h3[@class='has-primary-color has-text-color']")))
    
  • 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
    

CodePudding user response:

You can use method getText() and check test for empty.

  • Related