-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtab.py
More file actions
269 lines (233 loc) · 8.55 KB
/
tab.py
File metadata and controls
269 lines (233 loc) · 8.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
from typing import Optional
from webdriverbidi import execute
class Tab:
port: int
id: str
url: str
def __init__(self, port: int, id: str, url: str) -> None:
"""
Initialize a Tab instance.
Args:
port: The port number where the WebDriver BiDi server is running.
id: The unique identifier for the tab.
url: The current URL of the tab.
"""
self.port = port
self.id = id
self.url = url
def __str__(self) -> str:
return f"Tab(port={self.port}, id={self.id}, url={self.url})"
def navigate(self, url: str) -> bool:
"""
Navigate the tab to a new URL.
Args:
url: The URL to navigate to.
Returns:
True if navigation was successful, False otherwise.
"""
if result := execute(
self.port,
"browsingContext.navigate",
{"url": url, "context": self.id, "wait": "complete"},
):
self.url = result["url"]
return True
return False
def reload(self) -> bool:
"""
Reload this tab.
Returns:
True if reload was successful, False otherwise.
"""
if response := execute(
self.port,
"browsingContext.reload",
{
"context": self.id,
"wait": "complete",
},
):
self.url = response["url"]
return True
return False
def close(self) -> bool:
"""
Close this tab.
Returns:
True if the tab was closed successfully, False otherwise.
"""
return (
execute(self.port, "browsingContext.close", {"context": self.id})
is not None
)
def evaluate(self, script: str) -> Optional[dict]:
"""
Evaluate a JavaScript expression in the context of this tab.
Args:
script: The JavaScript expression to evaluate.
Returns:
The result of the evaluation, or None if evaluation failed.
"""
if response := execute(
self.port,
"script.evaluate",
{
"expression": script,
"target": {"context": self.id},
"awaitPromise": True,
},
):
print(f"Evaluated script: {script} -> {response}")
return response.get("result") or None
return None
def has_element_attribute(self, query: str, attribute: str) -> Optional[bool]:
"""
Check if an element has a specific attribute.
Args:
query: The JavaScript query to select the element.
attribute: The name of the attribute to check.
Returns:
True if the attribute exists, False if not, None if evaluation failed.
"""
if response := self.evaluate(f""" {query}.hasAttribute('{attribute}') """):
if response["type"] != "null":
return response["value"]
return None
def get_element_attribute(self, query: str, attribute: str) -> Optional[str]:
"""
Get the value of an attribute for an element selected by the query.
Args:
query: The JavaScript query to select the element.
attribute: The name of the attribute to retrieve.
Returns:
The value of the attribute if found, None otherwise.
"""
if response := self.evaluate(f""" {query}.{attribute} """):
if response["type"] != "null":
return response["value"]
return None
def set_element_attribute(self, query: str, attribute: str, value: str) -> bool:
"""
Set the value of an attribute for an element selected by the query.
Args:
query: The JavaScript query to select the element.
attribute: The name of the attribute to set.
value: The value to set for the attribute.
Returns:
True if the attribute was set successfully, False otherwise.
"""
return (
self.evaluate(
";".join(
[
f""" var element = {query} """,
f""" element.{attribute} = '{value}' """,
""" element.dispatchEvent(new Event('input')) """,
""" element.dispatchEvent(new Event('change')) """,
]
),
)
is not None
)
def remove_element_attribute(self, query: str, attribute: str) -> bool:
"""
Remove an attribute from an element selected by the query.
Args:
query: The JavaScript query to select the element.
attribute: The name of the attribute to remove.
Returns:
True if the attribute was removed successfully, False otherwise.
"""
return (
self.evaluate(f""" {query}.removeAttribute('{attribute}') """) is not None
)
def is_element_found(self, query: str) -> Optional[bool]:
"""
Check if an element exists in the DOM based on the provided query.
Args:
query: The JavaScript query to select the element.
Returns:
True if the element is found, False if not found, None if evaluation failed.
"""
if result := self.evaluate(query):
return result["type"] != "null"
return None
def is_element_displayed(self, query: str) -> Optional[bool]:
"""
Check if an element is displayed in the viewport.
Args:
query: The JavaScript query to select the element.
Returns:
True if the element is displayed, False if not displayed, None if evaluation failed.
"""
if response := self.evaluate(
";".join(
[
f""" var element = {query} """,
""" var rect = element.getBoundingClientRect() """,
"&&".join(
[
"rect.top >= 0",
"rect.left >= 0",
"rect.bottom <= (window.innerHeight || document.documentElement.clientHeight)",
"rect.right <= (window.innerWidth || document.documentElement.clientWidth)",
]
),
]
),
):
return response["value"]
return None
def is_element_disabled(self, query: str) -> Optional[bool]:
"""
Check if an element is disabled.
Args:
query: The JavaScript query to select the element.
Returns:
True if the element is disabled, False if not disabled, None if evaluation failed.
"""
return self.has_element_attribute(query, "disabled")
def is_element_equal_to(self, query1: str, query2: str) -> Optional[bool]:
"""
Check if two elements are equal based on their queries.
Args:
query1: The JavaScript query for the first element.
query2: The JavaScript query for the second element.
Returns:
True if the elements are equal, False if not equal, None if evaluation failed.
"""
if result := self.evaluate(f""" {query1} === {query2} """):
return result["value"]
return None
def focus_element(self, query: str) -> bool:
"""
Focus on an element selected by the query.
Args:
query: The JavaScript query to select the element.
Returns:
True if the element was focused successfully, False otherwise.
"""
return self.evaluate(f""" {query}.focus() """) is not None
def click_element(self, query: str) -> bool:
"""
Click on an element selected by the query.
Args:
query: The JavaScript query to select the element.
Returns:
True if the element was clicked successfully, False otherwise.
"""
return self.evaluate(f""" {query}.click() """) is not None
def scroll_element(self, query: str) -> bool:
"""
Scroll an element into view.
Args:
query: The JavaScript query to select the element.
Returns:
True if the element was scrolled into view successfully, False otherwise.
"""
return (
self.evaluate(
f""" {query}.scrollIntoView({{"block": "center", "inline": "nearest"}}) """
)
is not None
)