Skip to content

Commit eb6e5be

Browse files
committed
got it
1 parent 9ac0a92 commit eb6e5be

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

examples/selenium_basic.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from examples import bb, BROWSERBASE_PROJECT_ID, BROWSERBASE_API_KEY
2+
from browserbase.types import SessionCreateResponse
3+
from selenium import webdriver
4+
from selenium.webdriver.remote.webdriver import WebDriver
5+
from selenium.webdriver.remote.remote_connection import RemoteConnection
6+
7+
8+
class BrowserbaseConnection(RemoteConnection):
9+
"""
10+
Manage a single session with Browserbase.
11+
"""
12+
13+
browserbase_session: SessionCreateResponse
14+
15+
def __init__(self, *args, **kwargs):
16+
self.browserbase_session = bb.sessions.create(project_id=BROWSERBASE_PROJECT_ID)
17+
super().__init__(*args, **kwargs)
18+
19+
def get_remote_connection_headers(self, parsed_url, keep_alive=False):
20+
headers = super().get_remote_connection_headers(parsed_url, keep_alive)
21+
22+
# Update headers to include the Browserbase required information
23+
headers["x-bb-api-key"] = BROWSERBASE_API_KEY
24+
headers["session-id"] = self.browserbase_session.id
25+
26+
return headers
27+
28+
29+
def run(driver: WebDriver):
30+
# Instruct the browser to go to the SF MOMA page
31+
driver.get("https://www.sfmoma.org")
32+
33+
# Print out a bit of info about the page it landed on
34+
print(f"{driver.current_url=} | {driver.title=}")
35+
36+
...
37+
38+
39+
# Use the custom class to create and connect to a new browser session
40+
connection = BrowserbaseConnection("http://connect.browserbase.com/webdriver")
41+
driver = webdriver.Remote(connection, options=webdriver.ChromeOptions())
42+
43+
# Print a bit of info about the browser we've connected to
44+
print(
45+
"Connected to Browserbase",
46+
f"{driver.name} version {driver.caps['browserVersion']}",
47+
)
48+
49+
try:
50+
# Perform our browser commands
51+
run(driver)
52+
53+
finally:
54+
# Make sure to quit the driver so your session is ended!
55+
driver.quit()

pyproject.toml

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ dev-dependencies = [
5959
"rich>=13.7.1",
6060
"python-dotenv",
6161
"playwright",
62+
"selenium",
6263
]
6364

6465
[tool.rye.scripts]

requirements-dev.lock

+23
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ anyio==4.4.0
1818
argcomplete==3.1.2
1919
# via nox
2020
attrs==23.1.0
21+
# via outcome
2122
# via pytest
23+
# via trio
2224
certifi==2023.7.22
2325
# via httpcore
2426
# via httpx
2527
# via requests
28+
# via selenium
2629
charset-normalizer==3.4.0
2730
# via requests
2831
colorlog==6.7.0
@@ -34,12 +37,15 @@ distro==1.8.0
3437
# via browserbase
3538
exceptiongroup==1.1.3
3639
# via anyio
40+
# via trio
41+
# via trio-websocket
3742
filelock==3.12.4
3843
# via virtualenv
3944
greenlet==3.1.1
4045
# via playwright
4146
h11==0.14.0
4247
# via httpcore
48+
# via wsproto
4349
httpcore==1.0.2
4450
# via httpx
4551
httpx==0.25.2
@@ -49,6 +55,7 @@ idna==3.4
4955
# via anyio
5056
# via httpx
5157
# via requests
58+
# via trio
5259
importlib-metadata==7.0.0
5360
iniconfig==2.0.0
5461
# via pytest
@@ -62,6 +69,8 @@ mypy-extensions==1.0.0
6269
nodeenv==1.8.0
6370
# via pyright
6471
nox==2023.4.22
72+
outcome==1.3.0.post0
73+
# via trio
6574
packaging==23.2
6675
# via nox
6776
# via pytest
@@ -82,6 +91,8 @@ pyee==12.0.0
8291
pygments==2.18.0
8392
# via rich
8493
pyright==1.1.380
94+
pysocks==1.7.1
95+
# via urllib3
8596
pytest==7.1.1
8697
# via pytest-asyncio
8798
# via pytest-base-url
@@ -102,6 +113,7 @@ requests==2.32.3
102113
respx==0.20.2
103114
rich==13.7.1
104115
ruff==0.6.9
116+
selenium==4.16.0
105117
setuptools==68.2.2
106118
# via nodeenv
107119
six==1.16.0
@@ -110,12 +122,20 @@ sniffio==1.3.0
110122
# via anyio
111123
# via browserbase
112124
# via httpx
125+
# via trio
126+
sortedcontainers==2.4.0
127+
# via trio
113128
text-unidecode==1.3
114129
# via python-slugify
115130
time-machine==2.9.0
116131
tomli==2.0.1
117132
# via mypy
118133
# via pytest
134+
trio==0.24.0
135+
# via selenium
136+
# via trio-websocket
137+
trio-websocket==0.11.1
138+
# via selenium
119139
typing-extensions==4.8.0
120140
# via anyio
121141
# via browserbase
@@ -125,7 +145,10 @@ typing-extensions==4.8.0
125145
# via pyee
126146
urllib3==2.2.3
127147
# via requests
148+
# via selenium
128149
virtualenv==20.24.5
129150
# via nox
151+
wsproto==1.2.0
152+
# via trio-websocket
130153
zipp==3.17.0
131154
# via importlib-metadata

0 commit comments

Comments
 (0)