From 8fad922d88c005d1f9c63673963a74c1b6b30abb Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Thu, 13 Feb 2025 17:34:56 +0100 Subject: [PATCH] add selenium example --- selenium/README.md | 7 +++++++ selenium/cdp.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 selenium/README.md create mode 100644 selenium/cdp.py diff --git a/selenium/README.md b/selenium/README.md new file mode 100644 index 0000000..bc69f65 --- /dev/null +++ b/selenium/README.md @@ -0,0 +1,7 @@ +# Selenium examples using CDP + +## Prerequisites + +``` +$ pip install trio selenium +``` diff --git a/selenium/cdp.py b/selenium/cdp.py new file mode 100644 index 0000000..c65dd16 --- /dev/null +++ b/selenium/cdp.py @@ -0,0 +1,32 @@ +import trio +import sys +import logging +from selenium import webdriver +from selenium.webdriver.common.by import By + +logger = logging.getLogger('selenium') +logger.setLevel(logging.DEBUG) + +handler = logging.StreamHandler(sys.stderr) +logger.addHandler(handler) + +logging.getLogger('selenium.webdriver.remote').setLevel(logging.WARN) +logging.getLogger('selenium.webdriver.common').setLevel(logging.DEBUG) + +async def run(driver): + async with driver.bidi_connection() as session: + await trio.to_thread.run_sync(lambda: driver.get('https://blg.tch.re')) + + links = driver.find_elements(By.TAG_NAME, 'a') + + for a in links: + print(a.get_attribute("href")) + +options = webdriver.ChromeOptions() +options.page_load_strategy = 'normal' +options.enable_bidi = True +options.add_experimental_option("debuggerAddress", "127.0.0.1:9222") + +driver = webdriver.Chrome(options) + +trio.run(run, driver)