6

As the title suggests need to wait for dynamically loaded material. The material shows when scrolling to the end of the page each time.

Currently trying to do this by the following:

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
WebDriverWait(driver, 5).until(expected_conditions.presence_of_element_located(
    (By.XPATH, "//*[@id='inf-loader']/span[1][contains(@style, 'display: inline-block')]")))

Is the xpath the problem here (not all that familiar)? It keeps timing out even though the display style does change when scrolling with selenium.

Pythonista
  • 11,377
  • 2
  • 31
  • 50

1 Answers1

11

I would wait for the display style to change to inline-block with a custom Expected Condition and use value_of_css_property():

from selenium.webdriver.support import expected_conditions as EC

class wait_for_display(object):
    def __init__(self, locator):
        self.locator = locator

    def __call__(self, driver):
        try:
            element = EC._find_element(driver, self.locator)
            return element.value_of_css_property("display") == "inline-block"
        except StaleElementReferenceException:
            return False

Usage:

wait = WebDriverWait(driver, 5)
wait.until(wait_for_display((By.XPATH, "//*[@id='inf-loader']/span[1]")))

You may also try to tweak the poll frequency to make it check the condition more often:

wait = WebDriverWait(driver, 5, poll_frequency=0.1)  # default frequency is 0.5
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195