From 628ca0a84f77f24afab5f27471a4b6c4ea014425 Mon Sep 17 00:00:00 2001 From: Anirudh Kamath Date: Sun, 27 Oct 2024 16:11:12 -0700 Subject: [PATCH 1/5] got it --- examples/selenium_basic.py | 55 ++++++++++++++++++++++++++++++++++++++ pyproject.toml | 1 + requirements-dev.lock | 23 ++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 examples/selenium_basic.py diff --git a/examples/selenium_basic.py b/examples/selenium_basic.py new file mode 100644 index 0000000..a2cbdc3 --- /dev/null +++ b/examples/selenium_basic.py @@ -0,0 +1,55 @@ +from examples import bb, BROWSERBASE_PROJECT_ID, BROWSERBASE_API_KEY +from browserbase.types import SessionCreateResponse +from selenium import webdriver +from selenium.webdriver.remote.webdriver import WebDriver +from selenium.webdriver.remote.remote_connection import RemoteConnection + + +class BrowserbaseConnection(RemoteConnection): + """ + Manage a single session with Browserbase. + """ + + browserbase_session: SessionCreateResponse + + def __init__(self, *args, **kwargs): + self.browserbase_session = bb.sessions.create(project_id=BROWSERBASE_PROJECT_ID) + super().__init__(*args, **kwargs) + + def get_remote_connection_headers(self, parsed_url, keep_alive=False): + headers = super().get_remote_connection_headers(parsed_url, keep_alive) + + # Update headers to include the Browserbase required information + headers["x-bb-api-key"] = BROWSERBASE_API_KEY + headers["session-id"] = self.browserbase_session.id + + return headers + + +def run(driver: WebDriver): + # Instruct the browser to go to the SF MOMA page + driver.get("https://www.sfmoma.org") + + # Print out a bit of info about the page it landed on + print(f"{driver.current_url=} | {driver.title=}") + + ... + + +# Use the custom class to create and connect to a new browser session +connection = BrowserbaseConnection("http://connect.browserbase.com/webdriver") +driver = webdriver.Remote(connection, options=webdriver.ChromeOptions()) + +# Print a bit of info about the browser we've connected to +print( + "Connected to Browserbase", + f"{driver.name} version {driver.caps['browserVersion']}", +) + +try: + # Perform our browser commands + run(driver) + +finally: + # Make sure to quit the driver so your session is ended! + driver.quit() diff --git a/pyproject.toml b/pyproject.toml index cb341e0..052598c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -59,6 +59,7 @@ dev-dependencies = [ "rich>=13.7.1", "python-dotenv", "playwright", + "selenium", ] [tool.rye.scripts] diff --git a/requirements-dev.lock b/requirements-dev.lock index 3bd5de5..0be4077 100644 --- a/requirements-dev.lock +++ b/requirements-dev.lock @@ -18,11 +18,14 @@ anyio==4.4.0 argcomplete==3.1.2 # via nox attrs==23.1.0 + # via outcome # via pytest + # via trio certifi==2023.7.22 # via httpcore # via httpx # via requests + # via selenium charset-normalizer==3.4.0 # via requests colorlog==6.7.0 @@ -34,12 +37,15 @@ distro==1.8.0 # via browserbase exceptiongroup==1.1.3 # via anyio + # via trio + # via trio-websocket filelock==3.12.4 # via virtualenv greenlet==3.1.1 # via playwright h11==0.14.0 # via httpcore + # via wsproto httpcore==1.0.2 # via httpx httpx==0.25.2 @@ -49,6 +55,7 @@ idna==3.4 # via anyio # via httpx # via requests + # via trio importlib-metadata==7.0.0 iniconfig==2.0.0 # via pytest @@ -62,6 +69,8 @@ mypy-extensions==1.0.0 nodeenv==1.8.0 # via pyright nox==2023.4.22 +outcome==1.3.0.post0 + # via trio packaging==23.2 # via nox # via pytest @@ -82,6 +91,8 @@ pyee==12.0.0 pygments==2.18.0 # via rich pyright==1.1.380 +pysocks==1.7.1 + # via urllib3 pytest==7.1.1 # via pytest-asyncio # via pytest-base-url @@ -102,6 +113,7 @@ requests==2.32.3 respx==0.20.2 rich==13.7.1 ruff==0.6.9 +selenium==4.16.0 setuptools==68.2.2 # via nodeenv six==1.16.0 @@ -110,12 +122,20 @@ sniffio==1.3.0 # via anyio # via browserbase # via httpx + # via trio +sortedcontainers==2.4.0 + # via trio text-unidecode==1.3 # via python-slugify time-machine==2.9.0 tomli==2.0.1 # via mypy # via pytest +trio==0.24.0 + # via selenium + # via trio-websocket +trio-websocket==0.11.1 + # via selenium typing-extensions==4.8.0 # via anyio # via browserbase @@ -125,7 +145,10 @@ typing-extensions==4.8.0 # via pyee urllib3==2.2.3 # via requests + # via selenium virtualenv==20.24.5 # via nox +wsproto==1.2.0 + # via trio-websocket zipp==3.17.0 # via importlib-metadata From 5fe1b29e2dc0bb6d2416f042b40d8ef9614c7feb Mon Sep 17 00:00:00 2001 From: Anirudh Kamath Date: Sun, 27 Oct 2024 16:19:00 -0700 Subject: [PATCH 2/5] why are these headers necessary --- examples/selenium_basic.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/examples/selenium_basic.py b/examples/selenium_basic.py index a2cbdc3..85a9d39 100644 --- a/examples/selenium_basic.py +++ b/examples/selenium_basic.py @@ -11,17 +11,18 @@ class BrowserbaseConnection(RemoteConnection): """ browserbase_session: SessionCreateResponse + session_id: str - def __init__(self, *args, **kwargs): - self.browserbase_session = bb.sessions.create(project_id=BROWSERBASE_PROJECT_ID) + def __init__(self, session_id: str, *args, **kwargs): super().__init__(*args, **kwargs) + self.session_id = session_id def get_remote_connection_headers(self, parsed_url, keep_alive=False): headers = super().get_remote_connection_headers(parsed_url, keep_alive) # Update headers to include the Browserbase required information headers["x-bb-api-key"] = BROWSERBASE_API_KEY - headers["session-id"] = self.browserbase_session.id + headers["session-id"] = self.session_id return headers @@ -37,7 +38,8 @@ def run(driver: WebDriver): # Use the custom class to create and connect to a new browser session -connection = BrowserbaseConnection("http://connect.browserbase.com/webdriver") +session = bb.sessions.create(project_id=BROWSERBASE_PROJECT_ID) +connection = BrowserbaseConnection(session.id, session.selenium_remote_url) driver = webdriver.Remote(connection, options=webdriver.ChromeOptions()) # Print a bit of info about the browser we've connected to From 74e6d51ff72a0102bb6998d3d27f4e00c4cb8cfe Mon Sep 17 00:00:00 2001 From: Anirudh Kamath Date: Sun, 27 Oct 2024 18:53:06 -0700 Subject: [PATCH 3/5] basic selenium --- examples/e2e/test_playwright.py | 3 +- examples/e2e/test_selenium.py | 5 +++ examples/playwright_contexts.py | 6 ++-- examples/selenium_basic.py | 61 +++++++++++++++++---------------- 4 files changed, 42 insertions(+), 33 deletions(-) create mode 100644 examples/e2e/test_selenium.py diff --git a/examples/e2e/test_playwright.py b/examples/e2e/test_playwright.py index a08de60..199b803 100644 --- a/examples/e2e/test_playwright.py +++ b/examples/e2e/test_playwright.py @@ -20,9 +20,10 @@ load_dotenv() CI = os.getenv("CI", "false").lower() == "true" +MAX_RETRIES = 3 -@pytest.fixture(scope="session") +@pytest.fixture(scope="function") # Changed from "session" to "function" def playwright() -> Generator[Playwright, None, None]: with sync_playwright() as p: yield p diff --git a/examples/e2e/test_selenium.py b/examples/e2e/test_selenium.py new file mode 100644 index 0000000..9e64698 --- /dev/null +++ b/examples/e2e/test_selenium.py @@ -0,0 +1,5 @@ +from .. import selenium_basic + + +def test_selenium_basic() -> None: + selenium_basic.run() diff --git a/examples/playwright_contexts.py b/examples/playwright_contexts.py index 6a10846..cdd8551 100644 --- a/examples/playwright_contexts.py +++ b/examples/playwright_contexts.py @@ -1,5 +1,6 @@ import time from typing import Optional +from pydantic import TypeAdapter from playwright.sync_api import Cookie, Browser, Playwright, sync_playwright @@ -47,10 +48,11 @@ def run(playwright: Playwright) -> None: # Step 2: Creates a session with the context session = bb.sessions.create( project_id=BROWSERBASE_PROJECT_ID, - browser_settings=BrowserSettings( - context=BrowserSettingsContext(id=context_id, persist=True), + browser_settings=TypeAdapter(BrowserSettings).validate_python( + {"context": {"id": context_id, "persist": True}} ), ) + print(session) assert ( session.context_id == context_id diff --git a/examples/selenium_basic.py b/examples/selenium_basic.py index 85a9d39..da8ebfa 100644 --- a/examples/selenium_basic.py +++ b/examples/selenium_basic.py @@ -1,8 +1,7 @@ from examples import bb, BROWSERBASE_PROJECT_ID, BROWSERBASE_API_KEY -from browserbase.types import SessionCreateResponse from selenium import webdriver -from selenium.webdriver.remote.webdriver import WebDriver from selenium.webdriver.remote.remote_connection import RemoteConnection +from typing import Dict class BrowserbaseConnection(RemoteConnection): @@ -10,15 +9,16 @@ class BrowserbaseConnection(RemoteConnection): Manage a single session with Browserbase. """ - browserbase_session: SessionCreateResponse session_id: str - def __init__(self, session_id: str, *args, **kwargs): - super().__init__(*args, **kwargs) + def __init__(self, session_id: str, *args, **kwargs): # type: ignore + super().__init__(*args, **kwargs) # type: ignore self.session_id = session_id - def get_remote_connection_headers(self, parsed_url, keep_alive=False): - headers = super().get_remote_connection_headers(parsed_url, keep_alive) + def get_remote_connection_headers( # type: ignore + self, parsed_url: str, keep_alive: bool = False + ) -> Dict[str, str]: + headers = super().get_remote_connection_headers(parsed_url, keep_alive) # type: ignore # Update headers to include the Browserbase required information headers["x-bb-api-key"] = BROWSERBASE_API_KEY @@ -27,31 +27,32 @@ def get_remote_connection_headers(self, parsed_url, keep_alive=False): return headers -def run(driver: WebDriver): - # Instruct the browser to go to the SF MOMA page - driver.get("https://www.sfmoma.org") +def run(): + # Use the custom class to create and connect to a new browser session + session = bb.sessions.create(project_id=BROWSERBASE_PROJECT_ID) + connection = BrowserbaseConnection(session.id, session.selenium_remote_url) + driver = webdriver.Remote( + command_executor=connection, options=webdriver.ChromeOptions() # type: ignore + ) - # Print out a bit of info about the page it landed on - print(f"{driver.current_url=} | {driver.title=}") + # Print a bit of info about the browser we've connected to + print( + "Connected to Browserbase", + f"{driver.name} version {driver.caps['browserVersion']}", # type: ignore + ) - ... + try: + # Perform our browser commands + driver.get("https://www.sfmoma.org") + print(f"At URL: {driver.current_url} | Title: {driver.title}") + assert driver.current_url == "https://www.sfmoma.org/" + assert driver.title == "SFMOMA" + finally: + # Make sure to quit the driver so your session is ended! + print("Quitting driver") + # driver.quit() -# Use the custom class to create and connect to a new browser session -session = bb.sessions.create(project_id=BROWSERBASE_PROJECT_ID) -connection = BrowserbaseConnection(session.id, session.selenium_remote_url) -driver = webdriver.Remote(connection, options=webdriver.ChromeOptions()) -# Print a bit of info about the browser we've connected to -print( - "Connected to Browserbase", - f"{driver.name} version {driver.caps['browserVersion']}", -) - -try: - # Perform our browser commands - run(driver) - -finally: - # Make sure to quit the driver so your session is ended! - driver.quit() +if __name__ == "__main__": + run() From 477a09a820c3c0bb4ccf55d8ed35f710060efc1c Mon Sep 17 00:00:00 2001 From: Anirudh Kamath Date: Sun, 27 Oct 2024 19:11:55 -0700 Subject: [PATCH 4/5] e2e tests pass --- examples/__init__.py | 3 --- examples/playwright_captcha.py | 13 ++----------- examples/playwright_contexts.py | 15 +++------------ examples/playwright_downloads.py | 11 ++--------- examples/playwright_extensions.py | 10 ++-------- examples/playwright_proxy.py | 25 ++++++------------------- examples/playwright_upload.py | 14 ++------------ 7 files changed, 17 insertions(+), 74 deletions(-) diff --git a/examples/__init__.py b/examples/__init__.py index baf9883..377bade 100644 --- a/examples/__init__.py +++ b/examples/__init__.py @@ -8,9 +8,6 @@ load_dotenv(override=True) # Make sure we have the required environment variables -BROWSERBASE_CONNECT_URL = os.environ.get( - "BROWSERBASE_CONNECT_URL", "wss://connect.browserbase.com" -) _BROWSERBASE_API_KEY = os.environ.get("BROWSERBASE_API_KEY") if not _BROWSERBASE_API_KEY: raise ValueError("BROWSERBASE_API_KEY is not set in environment") diff --git a/examples/playwright_captcha.py b/examples/playwright_captcha.py index da76844..f699bde 100644 --- a/examples/playwright_captcha.py +++ b/examples/playwright_captcha.py @@ -1,12 +1,7 @@ from playwright.sync_api import Playwright, ConsoleMessage, sync_playwright -from examples import ( - BROWSERBASE_API_KEY, - BROWSERBASE_PROJECT_ID, - BROWSERBASE_CONNECT_URL, - bb, -) +from examples import BROWSERBASE_PROJECT_ID, bb DEFAULT_CAPTCHA_URL = "https://www.google.com/recaptcha/api2/demo" OVERRIDE_TIMEOUT = 60000 # 60 seconds, adjust as needed @@ -19,11 +14,7 @@ def run(playwright: Playwright) -> None: assert session.status == "RUNNING", f"Session status is {session.status}" # Connect to the remote session - connect_url = ( - f"{BROWSERBASE_CONNECT_URL}?sessionId={session.id}&apiKey={BROWSERBASE_API_KEY}" - ) - chromium = playwright.chromium - browser = chromium.connect_over_cdp(connect_url) + browser = playwright.chromium.connect_over_cdp(session.connect_url) context = browser.contexts[0] page = context.pages[0] diff --git a/examples/playwright_contexts.py b/examples/playwright_contexts.py index cdd8551..4d25eff 100644 --- a/examples/playwright_contexts.py +++ b/examples/playwright_contexts.py @@ -5,12 +5,7 @@ from playwright.sync_api import Cookie, Browser, Playwright, sync_playwright -from examples import ( - BROWSERBASE_API_KEY, - BROWSERBASE_PROJECT_ID, - BROWSERBASE_CONNECT_URL, - bb, -) +from examples import BROWSERBASE_PROJECT_ID, bb from browserbase.types.session_create_params import ( BrowserSettings, BrowserSettingsContext, @@ -61,9 +56,7 @@ def run(playwright: Playwright) -> None: # Step 3: Populates and persists the context print(f"Populating context {context_id} during session {session_id}") - connect_url = ( - f"{BROWSERBASE_CONNECT_URL}?sessionId={session_id}&apiKey={BROWSERBASE_API_KEY}" - ) + connect_url = session.connect_url browser = playwright.chromium.connect_over_cdp(connect_url) page = browser.contexts[0].pages[0] @@ -110,9 +103,7 @@ def run(playwright: Playwright) -> None: # Step 5: Uses context to find previous state print(f"Reusing context {context_id} during session {session_id}") - connect_url = ( - f"{BROWSERBASE_CONNECT_URL}?sessionId={session_id}&apiKey={BROWSERBASE_API_KEY}" - ) + connect_url = session.connect_url browser = playwright.chromium.connect_over_cdp(connect_url) page = browser.contexts[0].pages[0] diff --git a/examples/playwright_downloads.py b/examples/playwright_downloads.py index 3e1e340..55261c6 100644 --- a/examples/playwright_downloads.py +++ b/examples/playwright_downloads.py @@ -5,12 +5,7 @@ from playwright.sync_api import Playwright, sync_playwright -from examples import ( - BROWSERBASE_API_KEY, - BROWSERBASE_PROJECT_ID, - BROWSERBASE_CONNECT_URL, - bb, -) +from examples import BROWSERBASE_PROJECT_ID, bb download_re = re.compile(r"sandstorm-(\d{13})+\.mp3") @@ -27,9 +22,7 @@ def run(playwright: Playwright) -> None: assert session.status == "RUNNING", f"Session status is {session.status}" # Connect to the remote session - connect_url = ( - f"{BROWSERBASE_CONNECT_URL}?sessionId={session.id}&apiKey={BROWSERBASE_API_KEY}" - ) + connect_url = session.connect_url browser = playwright.chromium.connect_over_cdp(connect_url) context = browser.contexts[0] page = context.pages[0] diff --git a/examples/playwright_extensions.py b/examples/playwright_extensions.py index d12f47a..7bdb942 100644 --- a/examples/playwright_extensions.py +++ b/examples/playwright_extensions.py @@ -7,9 +7,7 @@ from playwright.sync_api import Page, Playwright, sync_playwright from examples import ( - BROWSERBASE_API_KEY, BROWSERBASE_PROJECT_ID, - BROWSERBASE_CONNECT_URL, bb, ) from browserbase.types import Extension, SessionCreateResponse @@ -100,9 +98,7 @@ def run(playwright: Playwright) -> None: extension_id=extension.id, ) - browser = playwright.chromium.connect_over_cdp( - f"{BROWSERBASE_CONNECT_URL}?apiKey={BROWSERBASE_API_KEY}&sessionId={session.id}" - ) + browser = playwright.chromium.connect_over_cdp(session.connect_url) context = browser.contexts[0] page = context.pages[0] check_for_message(page, expected_message) @@ -116,9 +112,7 @@ def run(playwright: Playwright) -> None: proxies=True, ) - browser = playwright.chromium.connect_over_cdp( - f"{BROWSERBASE_CONNECT_URL}?apiKey={BROWSERBASE_API_KEY}&sessionId={session_with_proxy.id}" - ) + browser = playwright.chromium.connect_over_cdp(session_with_proxy.connect_url) context = browser.contexts[0] page = context.pages[0] diff --git a/examples/playwright_proxy.py b/examples/playwright_proxy.py index 4ea6677..5b00dfc 100644 --- a/examples/playwright_proxy.py +++ b/examples/playwright_proxy.py @@ -5,7 +5,6 @@ from examples import ( BROWSERBASE_API_KEY, BROWSERBASE_PROJECT_ID, - BROWSERBASE_CONNECT_URL, bb, ) @@ -26,9 +25,7 @@ def check_proxy_bytes(session_id: str) -> None: def run_enable_via_create_session(playwright: Playwright) -> None: session = bb.sessions.create(project_id=BROWSERBASE_PROJECT_ID, proxies=True) - browser = playwright.chromium.connect_over_cdp( - f"{BROWSERBASE_CONNECT_URL}?apiKey={BROWSERBASE_API_KEY}&sessionId={session.id}" - ) + browser = playwright.chromium.connect_over_cdp(session.connect_url) context = browser.contexts[0] page = context.pages[0] @@ -45,9 +42,7 @@ def run_enable_via_create_session(playwright: Playwright) -> None: def run_enable_via_querystring_with_created_session(playwright: Playwright) -> None: session = bb.sessions.create(project_id=BROWSERBASE_PROJECT_ID, proxies=True) - browser = playwright.chromium.connect_over_cdp( - f"{BROWSERBASE_CONNECT_URL}?apiKey={BROWSERBASE_API_KEY}&sessionId={session.id}&enableProxy=true" - ) + browser = playwright.chromium.connect_over_cdp(session.connect_url) context = browser.contexts[0] page = context.pages[0] @@ -84,9 +79,7 @@ def run_geolocation_country(playwright: Playwright) -> None: ], ) - browser = playwright.chromium.connect_over_cdp( - f"{BROWSERBASE_CONNECT_URL}?apiKey={BROWSERBASE_API_KEY}&sessionId={session.id}" - ) + browser = playwright.chromium.connect_over_cdp(session.connect_url) context = browser.contexts[0] page = context.pages[0] @@ -113,9 +106,7 @@ def run_geolocation_state(playwright: Playwright) -> None: ], ) - browser = playwright.chromium.connect_over_cdp( - f"{BROWSERBASE_CONNECT_URL}?apiKey={BROWSERBASE_API_KEY}&sessionId={session.id}" - ) + browser = playwright.chromium.connect_over_cdp(session.connect_url) context = browser.contexts[0] page = context.pages[0] @@ -143,9 +134,7 @@ def run_geolocation_american_city(playwright: Playwright) -> None: ], ) - browser = playwright.chromium.connect_over_cdp( - f"{BROWSERBASE_CONNECT_URL}?apiKey={BROWSERBASE_API_KEY}&sessionId={session.id}" - ) + browser = playwright.chromium.connect_over_cdp(session.connect_url) context = browser.contexts[0] page = context.pages[0] @@ -172,9 +161,7 @@ def run_geolocation_non_american_city(playwright: Playwright) -> None: ], ) - browser = playwright.chromium.connect_over_cdp( - f"{BROWSERBASE_CONNECT_URL}?apiKey={BROWSERBASE_API_KEY}&sessionId={session.id}" - ) + browser = playwright.chromium.connect_over_cdp(session.connect_url) context = browser.contexts[0] page = context.pages[0] diff --git a/examples/playwright_upload.py b/examples/playwright_upload.py index c1a2237..da1e32c 100644 --- a/examples/playwright_upload.py +++ b/examples/playwright_upload.py @@ -2,12 +2,7 @@ from playwright.sync_api import Playwright, sync_playwright -from examples import ( - BROWSERBASE_API_KEY, - BROWSERBASE_PROJECT_ID, - BROWSERBASE_CONNECT_URL, - bb, -) +from examples import BROWSERBASE_PROJECT_ID, bb PATH_TO_UPLOAD = Path.cwd() / "examples" / "packages" / "logo.png" @@ -16,13 +11,8 @@ def run(playwright: Playwright) -> None: # Create a session session = bb.sessions.create(project_id=BROWSERBASE_PROJECT_ID) - # Construct the URL - url = ( - f"{BROWSERBASE_CONNECT_URL}?apiKey={BROWSERBASE_API_KEY}&sessionId={session.id}" - ) - # Connect to the browser - browser = playwright.chromium.connect_over_cdp(url) + browser = playwright.chromium.connect_over_cdp(session.connect_url) context = browser.contexts[0] page = context.pages[0] From 6eedef28475e2ac2763e52ae03efd7f6f27b4c3d Mon Sep 17 00:00:00 2001 From: Anirudh Kamath Date: Sun, 27 Oct 2024 19:14:58 -0700 Subject: [PATCH 5/5] lint --- examples/e2e/test_playwright.py | 1 + examples/playwright_captcha.py | 1 - examples/playwright_contexts.py | 4 +--- examples/playwright_downloads.py | 1 - examples/playwright_proxy.py | 1 - examples/selenium_basic.py | 13 +++++++------ 6 files changed, 9 insertions(+), 12 deletions(-) diff --git a/examples/e2e/test_playwright.py b/examples/e2e/test_playwright.py index 199b803..2e58c70 100644 --- a/examples/e2e/test_playwright.py +++ b/examples/e2e/test_playwright.py @@ -4,6 +4,7 @@ import pytest from dotenv import load_dotenv from playwright.sync_api import Playwright, sync_playwright + from browserbase import Browserbase from .. import ( diff --git a/examples/playwright_captcha.py b/examples/playwright_captcha.py index f699bde..7bc2ff4 100644 --- a/examples/playwright_captcha.py +++ b/examples/playwright_captcha.py @@ -1,6 +1,5 @@ from playwright.sync_api import Playwright, ConsoleMessage, sync_playwright - from examples import BROWSERBASE_PROJECT_ID, bb DEFAULT_CAPTCHA_URL = "https://www.google.com/recaptcha/api2/demo" diff --git a/examples/playwright_contexts.py b/examples/playwright_contexts.py index 4d25eff..610636f 100644 --- a/examples/playwright_contexts.py +++ b/examples/playwright_contexts.py @@ -1,17 +1,15 @@ import time from typing import Optional -from pydantic import TypeAdapter +from pydantic import TypeAdapter from playwright.sync_api import Cookie, Browser, Playwright, sync_playwright - from examples import BROWSERBASE_PROJECT_ID, bb from browserbase.types.session_create_params import ( BrowserSettings, BrowserSettingsContext, ) - CONTEXT_TEST_URL = "https://www.browserbase.com" SECOND = 1000 diff --git a/examples/playwright_downloads.py b/examples/playwright_downloads.py index 55261c6..5305d30 100644 --- a/examples/playwright_downloads.py +++ b/examples/playwright_downloads.py @@ -4,7 +4,6 @@ from playwright.sync_api import Playwright, sync_playwright - from examples import BROWSERBASE_PROJECT_ID, bb download_re = re.compile(r"sandstorm-(\d{13})+\.mp3") diff --git a/examples/playwright_proxy.py b/examples/playwright_proxy.py index 5b00dfc..8378e29 100644 --- a/examples/playwright_proxy.py +++ b/examples/playwright_proxy.py @@ -3,7 +3,6 @@ from playwright.sync_api import Page, Playwright, sync_playwright from examples import ( - BROWSERBASE_API_KEY, BROWSERBASE_PROJECT_ID, bb, ) diff --git a/examples/selenium_basic.py b/examples/selenium_basic.py index da8ebfa..83b7307 100644 --- a/examples/selenium_basic.py +++ b/examples/selenium_basic.py @@ -1,7 +1,9 @@ -from examples import bb, BROWSERBASE_PROJECT_ID, BROWSERBASE_API_KEY +from typing import Dict + from selenium import webdriver from selenium.webdriver.remote.remote_connection import RemoteConnection -from typing import Dict + +from examples import BROWSERBASE_API_KEY, BROWSERBASE_PROJECT_ID, bb class BrowserbaseConnection(RemoteConnection): @@ -24,10 +26,10 @@ def get_remote_connection_headers( # type: ignore headers["x-bb-api-key"] = BROWSERBASE_API_KEY headers["session-id"] = self.session_id - return headers + return headers # type: ignore -def run(): +def run() -> None: # Use the custom class to create and connect to a new browser session session = bb.sessions.create(project_id=BROWSERBASE_PROJECT_ID) connection = BrowserbaseConnection(session.id, session.selenium_remote_url) @@ -50,8 +52,7 @@ def run(): finally: # Make sure to quit the driver so your session is ended! - print("Quitting driver") - # driver.quit() + driver.quit() if __name__ == "__main__":