Skip to content

Commit d2ab626

Browse files
shbenzerdiemol
andauthored
[py] Upgraded WebDriverWait Docstrings (#15054)
* Improved WebDriverWait Docstrings * format.sh * Revert "format.sh" extra liting done This reverts commit 32214b6. * linting * Update wait.py * formatted again * ugh, format.sh again? * Update format.sh * Change Parameters to Attributes * accidentally replaced all --------- Co-authored-by: Diego Molina <[email protected]>
1 parent 790fd61 commit d2ab626

File tree

1 file changed

+87
-27
lines changed
  • py/selenium/webdriver/support

1 file changed

+87
-27
lines changed

py/selenium/webdriver/support/wait.py

Lines changed: 87 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,29 @@ def __init__(
4848
):
4949
"""Constructor, takes a WebDriver instance and timeout in seconds.
5050
51-
:Args:
52-
- driver - Instance of WebDriver (Ie, Firefox, Chrome or Remote) or a WebElement
53-
- timeout - Number of seconds before timing out
54-
- poll_frequency - sleep interval between calls
55-
By default, it is 0.5 second.
56-
- ignored_exceptions - iterable structure of exception classes ignored during calls.
57-
By default, it contains NoSuchElementException only.
58-
59-
Example::
60-
61-
from selenium.webdriver.support.wait import WebDriverWait \n
62-
element = WebDriverWait(driver, 10).until(lambda x: x.find_element(By.ID, "someId")) \n
63-
is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException)).\\ \n
64-
until_not(lambda x: x.find_element(By.ID, "someId").is_displayed())
51+
Attributes:
52+
----------
53+
driver
54+
- Instance of WebDriver (Ie, Firefox, Chrome or Remote) or
55+
a WebElement
56+
timeout
57+
- Number of seconds before timing out
58+
poll_frequency
59+
- Sleep interval between calls
60+
- By default, it is 0.5 second.
61+
ignored_exceptions
62+
- Iterable structure of exception classes ignored during calls.
63+
- By default, it contains NoSuchElementException only.
64+
65+
Example:
66+
--------
67+
>>> from selenium.webdriver.common.by import By
68+
>>> from selenium.webdriver.support.wait import WebDriverWait
69+
>>> from selenium.common.exceptions import ElementNotVisibleException
70+
>>>
71+
>>> # Wait until the element is no longer visible
72+
>>> is_disappeared = WebDriverWait(driver, 30, 1, (ElementNotVisibleException))
73+
... .until_not(lambda x: x.find_element(By.ID, "someId").is_displayed())
6574
"""
6675
self._driver = driver
6776
self._timeout = float(timeout)
@@ -81,13 +90,39 @@ def __repr__(self):
8190
return f'<{type(self).__module__}.{type(self).__name__} (session="{self._driver.session_id}")>'
8291

8392
def until(self, method: Callable[[D], Union[Literal[False], T]], message: str = "") -> T:
84-
"""Calls the method provided with the driver as an argument until the \
93+
"""Wait until the method returns a value that is not False.
94+
95+
Calls the method provided with the driver as an argument until the
8596
return value does not evaluate to ``False``.
8697
87-
:param method: callable(WebDriver)
88-
:param message: optional message for :exc:`TimeoutException`
89-
:returns: the result of the last call to `method`
90-
:raises: :exc:`selenium.common.exceptions.TimeoutException` if timeout occurs
98+
Parameters:
99+
----------
100+
method: callable(WebDriver)
101+
- A callable object that takes a WebDriver instance as an argument.
102+
message: str
103+
- Optional message for :exc:`TimeoutException`
104+
105+
Return:
106+
-------
107+
object: T
108+
- The result of the last call to `method`
109+
110+
Raises:
111+
-------
112+
TimeoutException
113+
- If 'method' does not return a truthy value within the WebDriverWait
114+
object's timeout
115+
116+
Example:
117+
--------
118+
>>> from selenium.webdriver.common.by import By
119+
>>> from selenium.webdriver.support.ui import WebDriverWait
120+
>>> from selenium.webdriver.support import expected_conditions as EC
121+
122+
# Wait until an element is visible on the page
123+
>>> wait = WebDriverWait(driver, 10)
124+
>>> element = wait.until(EC.visibility_of_element_located((By.ID, "exampleId")))
125+
>>> print(element.text)
91126
"""
92127
screen = None
93128
stacktrace = None
@@ -107,14 +142,39 @@ def until(self, method: Callable[[D], Union[Literal[False], T]], message: str =
107142
raise TimeoutException(message, screen, stacktrace)
108143

109144
def until_not(self, method: Callable[[D], T], message: str = "") -> Union[T, Literal[True]]:
110-
"""Calls the method provided with the driver as an argument until the \
111-
return value evaluates to ``False``.
112-
113-
:param method: callable(WebDriver)
114-
:param message: optional message for :exc:`TimeoutException`
115-
:returns: the result of the last call to `method`, or
116-
``True`` if `method` has raised one of the ignored exceptions
117-
:raises: :exc:`selenium.common.exceptions.TimeoutException` if timeout occurs
145+
"""Wait until the method returns a value that is not False.
146+
147+
Calls the method provided with the driver as an argument until the
148+
return value does not evaluate to ``False``.
149+
150+
Parameters:
151+
----------
152+
method: callable(WebDriver)
153+
- A callable object that takes a WebDriver instance as an argument.
154+
message: str
155+
- Optional message for :exc:`TimeoutException`
156+
157+
Return:
158+
-------
159+
object: T
160+
- The result of the last call to `method`
161+
162+
Raises:
163+
-------
164+
TimeoutException
165+
- If 'method' does not return False within the WebDriverWait
166+
object's timeout
167+
168+
Example:
169+
--------
170+
>>> from selenium.webdriver.common.by import By
171+
>>> from selenium.webdriver.support.ui import WebDriverWait
172+
>>> from selenium.webdriver.support import expected_conditions as EC
173+
174+
# Wait until an element is visible on the page
175+
>>> wait = WebDriverWait(driver, 10)
176+
>>> is_disappeared = wait.until_not(EC.visibility_of_element_located(
177+
... (By.ID, "exampleId")))
118178
"""
119179
end_time = time.monotonic() + self._timeout
120180
while True:

0 commit comments

Comments
 (0)