0

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

Rasmus
  • 45
  • 5

1 Answers1

0

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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you! I can't get either of the locator straties working. Also could you provide a locator strategy for checking if a specific text string is visible instead? Because there are multiple text strings using that class. – Rasmus Jul 31 '22 at 21:55
  • _visible to the user on the web page_ -> `visibility_of_element_located()` – undetected Selenium Jul 31 '22 at 21:58