robert arles robert.arles.us

robert.arles.us

Github Twitter?
about1 android1 backlight1 bash1 bayesian probability1 bayesian probablilty1 browsermob2 django5 docker1 firstpost1 flask1 go2 golang2 hugo1 init1 java1 jenkins2 jinja1 keyboard1 linux1 lubuntu1 mitmproxy1 nerdlynews1 nginx1 nltk1 pageloadstats2 pelican1 python11 qa automation1 rabot2 saucelabs1 selenium4 threading1 ubuntu2 webdriver4

Selenium

Waiting for elements on dynamic webpages Selenium / Webdriver

Below is my current favorite method to wait for an element to appear or become useful on a dynamic web page. In this case, my example is avoiding the exception thrown when webdriver fails to find an element by using the ‘find_elements’(plural) method rather than a ‘find_element’(singular) The ‘find_elements’ methods always return a list, even if empty, rather than throw an exception. Both are useful, but in this case I like the more readable code without the try/except requirements. I also like to have a way to record wait times for specific events. Here I’m just printing that info out for example, but I often record this in a db for charting or analysis later.

    SOME_CSS_SELECTOR = '#interesting_element_selector'
    MAX_WAIT = 5 # my style, this is often defined as a class constant
    .
    .
    .
    waited = 0
    while waited < self.MAX_WAIT:
    # wait for something. I most often put 'sleep(0) just before the 'waited+=1'
    # but put it here in cases where I know there is a slow element load
    sleep(1)
    elements = self.driver.find_elements_by_css_selector(self.SOME_CSS_SELECTOR)
    if len(elements) > 1:
    break
    else:
    loading_indicator_element = loading_indicator_elements[0]
    waited += 1
    print '[INFO] Waited %i seconds for %s' % (waited, str(self.SOME_CSS_SELECTOR))

-