Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make selenium driver wait until elements style attribute has changed

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.

like image 320
Pythonista Avatar asked Jan 21 '16 04:01

Pythonista


People also ask

How do you get Selenium to wait until the element is present?

Selenium: Waiting Until the Element Is Visiblevar wait = new WebDriverWait(driver, TimeSpan. FromSeconds(20)); As you can see, we give the WebDriverWait object two parameters: the driver itself and a TimeSpan object that represents the timeout for trying to locate the element.

How do I make Selenium wait a few seconds?

How can I ask the Selenium-WebDriver to wait for few seconds in Java? To tell the Selenium WebDriver to wait for a specified amount of time use Thread. sleep. This will tell Java to pause execution of the test for the specified number of milliseconds.

How wait until element is not present in Selenium?

This can be achieved with synchronization in Selenium. We shall add an explicit wait criteria where we shall stop or wait till the element no longer exists. Timeout exception is thrown once the explicit wait time has elapsed and the expected behavior of the element is still not available on the page.


1 Answers

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
like image 176
alecxe Avatar answered Nov 15 '22 06:11

alecxe