Search code examples
pythonclassselenium-webdriverwait

What is the most efficient way to wait for a page element (xpath) to show up in Selenium Webdriver with Python?


What is the most efficient way to wait for a page element (xpath) to show up in Selenium Webdriver with Python?

webdriver wait until, how do you use it when waiting for a class type to appear?


Solution

  • I believe Selenium has inbuild methods to check if an element is enabled or displayed. Try to play around with

    elem.is_enabled and elem.is_displayed

    Otherwise, you can use a while loop with a try/catch:

    while True:
        try:
            elem = driver.find_by....
            break
        except:
            time.sleep(1)
    

    In this case, the script will only proceed when the element is found

    :)