Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/common/timeouts.py
4009 views
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
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
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
16
# under the License.
17
18
from typing import TYPE_CHECKING
19
20
if TYPE_CHECKING:
21
from typing import TypedDict
22
23
class JSONTimeouts(TypedDict, total=False):
24
implicit: int
25
pageLoad: int
26
script: int
27
28
else:
29
JSONTimeouts = dict[str, int]
30
31
32
class _TimeoutsDescriptor:
33
"""Get or set the value of the attributes listed below.
34
35
_implicit_wait _page_load _script
36
37
This does not set the value on the remote end.
38
"""
39
40
def __init__(self, name):
41
self.name = name
42
43
def __get__(self, obj, cls) -> float:
44
return getattr(obj, self.name) / 1000
45
46
def __set__(self, obj, value) -> None:
47
converted_value = getattr(obj, "_convert")(value)
48
setattr(obj, self.name, converted_value)
49
50
51
class Timeouts:
52
def __init__(self, implicit_wait: float = 0, page_load: float = 0, script: float = 0) -> None:
53
"""Create a new Timeouts object.
54
55
This implements https://w3c.github.io/webdriver/#timeouts.
56
57
Args:
58
implicit_wait: Number of seconds to wait when searching for elements
59
before throwing an error.
60
page_load: Number of seconds to wait for a page load to complete
61
before throwing an error.
62
script: Number of seconds to wait for an asynchronous script to
63
finish execution before throwing an error.
64
"""
65
self._implicit_wait = self._convert(implicit_wait)
66
self._page_load = self._convert(page_load)
67
self._script = self._convert(script)
68
69
# Creating descriptor objects
70
implicit_wait = _TimeoutsDescriptor("_implicit_wait")
71
"""Number of seconds to wait when searching for elements.
72
73
Note: This does not set the value on the remote end.
74
"""
75
76
page_load = _TimeoutsDescriptor("_page_load")
77
"""Number of seconds to wait for the page to load.
78
79
Note: This does not set the value on the remote end.
80
"""
81
82
script = _TimeoutsDescriptor("_script")
83
"""Number of seconds to wait for an asynchronous script to finish execution.
84
85
Note: This does not set the value on the remote end.
86
"""
87
88
def _convert(self, timeout: float) -> int:
89
if isinstance(timeout, (int, float)):
90
return int(float(timeout) * 1000)
91
raise TypeError("Timeouts can only be an int or a float")
92
93
def _to_json(self) -> JSONTimeouts:
94
timeouts: JSONTimeouts = {}
95
if self._implicit_wait:
96
timeouts["implicit"] = self._implicit_wait
97
if self._page_load:
98
timeouts["pageLoad"] = self._page_load
99
if self._script:
100
timeouts["script"] = self._script
101
102
return timeouts
103
104