1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
9
# http://www.apache.org/licenses/LICENSE-2.0
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
19
from selenium.common.exceptions import NoSuchElementException
20
from selenium.common.exceptions import TimeoutException
22
POLL_FREQUENCY = 0.5 # How long to sleep inbetween calls to the method
23
IGNORED_EXCEPTIONS = (NoSuchElementException,) # exceptions ignored during calls to the method
26
class WebDriverWait(object):
27
def __init__(self, driver, timeout, poll_frequency=POLL_FREQUENCY, ignored_exceptions=None):
28
"""Constructor, takes a WebDriver instance and timeout in seconds.
31
- driver - Instance of WebDriver (Ie, Firefox, Chrome or Remote)
32
- timeout - Number of seconds before timing out
33
- poll_frequency - sleep interval between calls
34
By default, it is 0.5 second.
35
- ignored_exceptions - iterable structure of exception classes ignored during calls.
36
By default, it contains NoSuchElementException only.
39
from selenium.webdriver.support.ui import WebDriverWait \n
40
element = WebDriverWait(driver, 10).until(lambda x: x.find_element_by_id("someId")) \n
41
is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).\ \n
42
until_not(lambda x: x.find_element_by_id("someId").is_displayed())
45
self._timeout = timeout
46
self._poll = poll_frequency
47
# avoid the divide by zero
49
self._poll = POLL_FREQUENCY
50
exceptions = list(IGNORED_EXCEPTIONS)
51
if ignored_exceptions is not None:
53
exceptions.extend(iter(ignored_exceptions))
54
except TypeError: # ignored_exceptions is not iterable
55
exceptions.append(ignored_exceptions)
56
self._ignored_exceptions = tuple(exceptions)
59
return '<{0.__module__}.{0.__name__} (session="{1}")>'.format(
60
type(self), self._driver.session_id)
62
def until(self, method, message=''):
63
"""Calls the method provided with the driver as an argument until the \
64
return value is not False."""
68
end_time = time.time() + self._timeout
71
value = method(self._driver)
74
except self._ignored_exceptions as exc:
75
screen = getattr(exc, 'screen', None)
76
stacktrace = getattr(exc, 'stacktrace', None)
77
time.sleep(self._poll)
78
if time.time() > end_time:
80
raise TimeoutException(message, screen, stacktrace)
82
def until_not(self, method, message=''):
83
"""Calls the method provided with the driver as an argument until the \
84
return value is False."""
85
end_time = time.time() + self._timeout
88
value = method(self._driver)
91
except self._ignored_exceptions:
93
time.sleep(self._poll)
94
if time.time() > end_time:
96
raise TimeoutException(message)