-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser.py
More file actions
46 lines (37 loc) · 1.22 KB
/
browser.py
File metadata and controls
46 lines (37 loc) · 1.22 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
from typing import Optional
from tab import Tab
from webdriverbidi import execute
class Browser:
port: int
def __init__(self, port: int) -> None:
"""
Initialize a Browser instance.
Args:
port: The port number where the WebDriver BiDi server is running.
"""
self.port = port
def get_tabs(self) -> list[Tab]:
"""
Get the list of currently open tabs in the browser.
Returns:
A list of Tab objects representing the open tabs.
"""
if response := execute(self.port, "browsingContext.getTree", {"maxDepth": 1}):
return [
Tab(port=self.port, id=context["context"], url=context["url"])
for context in response["contexts"]
]
return []
def create_tab(self) -> Optional[Tab]:
"""
Create a new tab in the browser.
Returns:
A Tab object representing the newly created tab, or None if creation failed.
"""
if context := execute(
self.port,
"browsingContext.create",
{"type": "tab"},
):
return Tab(port=self.port, id=context["context"], url="")
return None