-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Description
🎭 Stealthy Playwright Mode is a special mode of SeleniumBase that launches Playwright from SeleniumBase CDP Mode in order to grant Playwright new stealth features, such as the ability to click CAPTCHA checkboxes successfully. Playwright uses connect_over_cdp() to attach itself onto an existing SeleniumBase session via the remote-debugging-port. From here, APIs of both frameworks can be used, giving you a hybrid approach that delivers the best experience of both worlds.
Stealthy Playwright Mode comes in 3 formats:
sb_cdpsync formatSBnested sync formatcdp_driverasync format
The following examples will work starting in SeleniumBase 4.45.0:
sb_cdp sync format (minimal boilerplate):
from playwright.sync_api import sync_playwright
from seleniumbase import sb_cdp
sb = sb_cdp.Chrome()
endpoint_url = sb.get_endpoint_url()
with sync_playwright() as p:
browser = p.chromium.connect_over_cdp(endpoint_url)
context = browser.contexts[0]
page = context.pages[0]
page.goto("https://example.com")SB nested sync format (minimal boilerplate):
from playwright.sync_api import sync_playwright
from seleniumbase import SB
with SB(uc=True) as sb:
sb.activate_cdp_mode()
endpoint_url = sb.cdp.get_endpoint_url()
with sync_playwright() as p:
browser = p.chromium.connect_over_cdp(endpoint_url)
context = browser.contexts[0]
page = context.pages[0]
page.goto("https://example.com")cdp_driver async format (minimal boilerplate):
import asyncio
from seleniumbase import cdp_driver
from playwright.async_api import async_playwright
async def main():
driver = await cdp_driver.start_async()
endpoint_url = driver.get_endpoint_url()
async with async_playwright() as p:
browser = await p.chromium.connect_over_cdp(endpoint_url)
context = browser.contexts[0]
page = context.pages[0]
await page.goto("https://example.com")
if __name__ == "__main__":
loop = asyncio.new_event_loop()
loop.run_until_complete(main())With the APIs combined, you'll be able to call SeleniumBase methods such as solve_captcha() from within your Playwright scripts:
from playwright.sync_api import sync_playwright
from seleniumbase import sb_cdp
sb = sb_cdp.Chrome(locale="en")
endpoint_url = sb.get_endpoint_url()
with sync_playwright() as p:
browser = p.chromium.connect_over_cdp(endpoint_url)
context = browser.contexts[0]
page = context.pages[0]
page.goto("https://www.bing.com/turing/captcha/challenge")
sb.sleep(3)
sb.solve_captcha()
sb.sleep(3)(Sometimes the CAPTCHA will be solved automatically without needing to call the solve_captcha() method)
Expect a lot more documentation once this feature is released.