Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/common/timeouts.py
1864 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 - Either an int or a float. Set how many
59
seconds to wait when searching for elements before
60
throwing an error.
61
- page_load - Either an int or a float. Set how many seconds
62
to wait for a page load to complete before throwing
63
an error.
64
- script - Either an int or a float. Set how many seconds to
65
wait for an asynchronous script to finish execution
66
before throwing an error.
67
"""
68
69
self._implicit_wait = self._convert(implicit_wait)
70
self._page_load = self._convert(page_load)
71
self._script = self._convert(script)
72
73
# Creating descriptor objects
74
implicit_wait = _TimeoutsDescriptor("_implicit_wait")
75
"""Get or set how many seconds to wait when searching for elements.
76
77
This does not set the value on the remote end.
78
79
Usage:
80
------
81
- Get
82
- `self.implicit_wait`
83
- Set
84
- `self.implicit_wait` = `value`
85
86
Parameters:
87
-----------
88
`value`: `float`
89
"""
90
91
page_load = _TimeoutsDescriptor("_page_load")
92
"""Get or set how many seconds to wait for the page to load.
93
94
This does not set the value on the remote end.
95
96
Usage:
97
------
98
- Get
99
- `self.page_load`
100
- Set
101
- `self.page_load` = `value`
102
103
Parameters:
104
-----------
105
`value`: `float`
106
"""
107
108
script = _TimeoutsDescriptor("_script")
109
"""Get or set how many seconds to wait for an asynchronous script to finish
110
execution.
111
112
This does not set the value on the remote end.
113
114
Usage:
115
------
116
- Get
117
- `self.script`
118
- Set
119
- `self.script` = `value`
120
121
Parameters:
122
-----------
123
`value`: `float`
124
"""
125
126
def _convert(self, timeout: float) -> int:
127
if isinstance(timeout, (int, float)):
128
return int(float(timeout) * 1000)
129
raise TypeError("Timeouts can only be an int or a float")
130
131
def _to_json(self) -> JSONTimeouts:
132
timeouts: JSONTimeouts = {}
133
if self._implicit_wait:
134
timeouts["implicit"] = self._implicit_wait
135
if self._page_load:
136
timeouts["pageLoad"] = self._page_load
137
if self._script:
138
timeouts["script"] = self._script
139
140
return timeouts
141
142