Skip to content

Use SeleniumBase with already opened Chrome by remote debugging address #3354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vannguyen799 opened this issue Dec 20, 2024 · 2 comments
Closed
Labels
question Someone is looking for answers UC Mode / CDP Mode Undetected Chromedriver Mode / CDP Mode

Comments

@vannguyen799
Copy link

vannguyen799 commented Dec 20, 2024

any solution to connect SeleniumBase with already opened Chrome by remote debugging address

Example in selenium:

chrome_service = ChromeService(driver_path)
chrome_options = Options()

chrome_options.debugger_address = f'{remote_debugging_address}'
driver = webdriver.Chrome(service=chrome_service, options=chrome_options)
@tomyo
Copy link

tomyo commented Dec 20, 2024

I was about to ask a similar question, would like this to run on a browser session with WhatsApp logged in, so I can read a public group messages... That would be awesome.

@mdmintz mdmintz added question Someone is looking for answers UC Mode / CDP Mode Undetected Chromedriver Mode / CDP Mode labels Dec 20, 2024
@mdmintz
Copy link
Member

mdmintz commented Dec 20, 2024

That's doable if you already know the host (should be "127.0.0.1") and the remote-debugging port.

Here's an async example:
(assuming there's already an active Chrome instance using 9222 as the remote debugging port)

import asyncio
from seleniumbase.undetected.cdp_driver import cdp_util

async def main():
    host = "127.0.0.1"
    port = 9222
    driver = await cdp_util.start_async(host=host, port=port)
    page = await driver.get("https://seleniumbase.io/")
    await asyncio.sleep(2)
    print(await page.evaluate("document.title"))
    element = await page.find("CDP Mode")
    await element.click_async()
    await asyncio.sleep(2)
    print(await page.evaluate("document.title"))

if __name__ == "__main__":
    asyncio.run(main())

If you're unfamiliar with async/await syntax, here's the same example converted into long sync format:

import asyncio
import time
from seleniumbase.undetected.cdp_driver import cdp_util

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
host = "127.0.0.1"
port = 9222
driver = loop.run_until_complete(cdp_util.start(host=host, port=port))
page = loop.run_until_complete(driver.get("https://seleniumbase.io/"))
time.sleep(2)
print(loop.run_until_complete(page.evaluate("document.title")))
element = loop.run_until_complete(page.find("CDP Mode"))
loop.run_until_complete(element.click_async())
time.sleep(2)
print(loop.run_until_complete(page.evaluate("document.title")))

And here's the simplified SB-CDP sync format for that:

import asyncio
from seleniumbase.core import sb_cdp
from seleniumbase.undetected.cdp_driver import cdp_util

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
host = "127.0.0.1"
port = 9222
driver = loop.run_until_complete(cdp_util.start(host=host, port=port))
page = loop.run_until_complete(driver.get("https://seleniumbase.io/"))
sb = sb_cdp.CDPMethods(loop, page, driver)
sb.sleep(2)
print(sb.get_title())
sb.click("CDP Mode")
sb.sleep(2)
print(sb.get_title())

Those examples use pure CDP:

Note that you'll get errors running those examples if there's no active Chrome instance with the debug port you selected.
For regular SB CDP Mode, new Chrome instances will first attempt to use debug port 9222 if not already taken.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Someone is looking for answers UC Mode / CDP Mode Undetected Chromedriver Mode / CDP Mode
Projects
None yet
Development

No branches or pull requests

3 participants