Search code examples
pythonseleniumselenium-webdriverwebdriververify

Verify text - innerHTML from element using Selenium webdriver with python


Trying to verify text presence, not assert just verify

My attempt:

expected_footer = "© 2020 Sauce Labs. All Rights Reserved. Terms of Service | Privacy Policy"
footer_text = driver.find_elements_by_xpath("//footer/div")
if expected_footer in footer_text:
    print("text visible...")
else:
    pass

Solution

  • Presumably, there will be a single footer with a webpage, so essentially instead of find_elements* you need to use find_element* and you can use the following solution:

    expected_footer = "© 2020 Sauce Labs. All Rights Reserved. Terms of Service | Privacy Policy"
    if expected_footer in WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//footer/div"))).text :
        print("text visible...")
    else:
        pass
    

    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