14

In java selenium-webdriver package, there is a FluentWait class:

Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.

In other words, it is something more than implicit and explicit wait, gives you more control for waiting for an element. It can be very handy and definitely has use cases.

Is there anything similar in python selenium package, or should I implement it myself?

(I've looked through documentation for Waits - nothing there).

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195

4 Answers4

25

I believe you can do this with Python, however it isn't packaged as simply as a FluentWait class. Some of this was covered in the documentation you provided by not extensively.

The WebDriverWait class has optional arguments for timeout, poll_frequency, and ignored_exceptions. So you could supply it there. Then combine it with an Expected Condition to wait for elements for appear, be clickable, etc... Here is an example:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import *

driver = webdriver.Firefox()
# Load some webpage
wait = WebDriverWait(driver, 10, poll_frequency=1, ignored_exceptions=[ElementNotVisibleException, ElementNotSelectableException])
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//div")))

Obviously you can combine the wait/element into one statement but I figured this way you can see where this is implemented.

  • Python 3: `WebDriverWait(self.driver, 17, poll_frequency=2, ignored_exceptions=[ElementNotVisibleException, ElementNotSelectableException]).until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "body")))` – ingyhere Oct 01 '20 at 07:53
8

iChar's answer covers how to use WebDriverWait in Python to do what FluentWait does in Java. Some aspects of the question were left unaddressed though:

In other words, [FluentWait] is something more than implicit and explicit wait

No. As of version 2.42.x of Selenium, there are only two kinds of waits that Selenium implements: implicit and explicit. FluentWait is not something additional to these two kinds of wait. It is just an explicit wait.

Is there anything similar in python selenium package, or should I implement it myself?

The only thing I can think of that is missing from Python's WebDriverWait implementation that FluentWait (and WebDriverWait, by extension) has, is this:

[FluentWait (and, by extension, WebDriverWait)] may have its timeout and polling interval configured on the fly.

[Quoted from this.]

The WebDriverWait class in Python is designed in such a way that its configuration values are set once and for all when it is created. FluentWait allows its configuration to be changed after creation. So a single FluentWait object (or any WebDriverWait in Java) could be reused to wait for different conditions with different polling frequencies. In Python, you'd have to create a new WebDriverWait object to use a different polling frequency.

So there is something the Python implementation does not provide but I would not consider this significant enough to warrant an implementation.

Louis
  • 146,715
  • 28
  • 274
  • 320
  • 1
    This makes things much more clear, thank you. I guess I would need another bounty here :) – alecxe Sep 11 '14 at 12:06
  • You could just adjust the class attributes if you need to change the timeout, polling, or ignored exceptions for a webdriver wait if you want to re-use the same one. Looking at the source for `WebDriverWait` there is nothing that indicates once it's initialized those values can't be changed, i.e. `WebDriverWait._timeout`. However I guess that my not really be considered "on-the-fly". –  Sep 11 '14 at 12:57
  • @iChar Although Python won't prevent you from assigning new values to these variables, that they are prefixed with an underscore does in fact mark them as "private" and tells you that these variables are not there for you to change. If I were to suggest that one should change them, then I'd be giving **very bad advice**. – Louis Sep 11 '14 at 13:01
1

The above implementation didn’t work well in my use case so I would share my implementation as well. For fluent wait, it is also nice to check some other criteria when polling, for example, we can check if there an attribute of an element has changed. So, in my condition the page should be refreshed. Below is the code [it is adjusted for 30 seconds(6 times the loop and 5 times for each second)]:

element = None
i = 6
while element is None:
    try:
        wait = WebDriverWait(driver, 5, poll_frequency=1)
        element = wait.until(expected_conditions.visibility_of_element_located(el))
    except:
        driver.refresh()
        i = i - 1
        print(i)
        if i < 0:
            raise Exception('Element not found')
1

As one of the above answer, The WebDriverWait class has optional arguments for timeout, poll_frequency, and ignored_exceptions. Moreover, if available expected conditions do not meet your needs, you can create custom wait conditions as in the document here https://selenium-python.readthedocs.io/waits.html Below is the code to poll until the element has expected text

wait=WebDriverWait(driver,timeout=10,poll_frequency=1)
element=wait.until(element_has_text((By.CSS_SELECTOR,"div#finish>h4"),"Hello World!"))

class element_has_text(object):
"""An expectation for checking that an element has a particular text.

locator - used to find the element
returns the WebElement once it has the particular text
"""
def __init__(self,locator,expected_text):
    self.locator=locator
    self.expected_text=expected_text

def __call__(self,driver):
    element = driver.find_element(*self.locator)
    if (element.text==self.expected_text):
        return element
    else:
        return False
Tran Van
  • 11
  • 1