Skip to content

Commit 8fad922

Browse files
committed
add selenium example
1 parent d2ea72a commit 8fad922

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

selenium/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Selenium examples using CDP
2+
3+
## Prerequisites
4+
5+
```
6+
$ pip install trio selenium
7+
```

selenium/cdp.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import trio
2+
import sys
3+
import logging
4+
from selenium import webdriver
5+
from selenium.webdriver.common.by import By
6+
7+
logger = logging.getLogger('selenium')
8+
logger.setLevel(logging.DEBUG)
9+
10+
handler = logging.StreamHandler(sys.stderr)
11+
logger.addHandler(handler)
12+
13+
logging.getLogger('selenium.webdriver.remote').setLevel(logging.WARN)
14+
logging.getLogger('selenium.webdriver.common').setLevel(logging.DEBUG)
15+
16+
async def run(driver):
17+
async with driver.bidi_connection() as session:
18+
await trio.to_thread.run_sync(lambda: driver.get('https://blg.tch.re'))
19+
20+
links = driver.find_elements(By.TAG_NAME, 'a')
21+
22+
for a in links:
23+
print(a.get_attribute("href"))
24+
25+
options = webdriver.ChromeOptions()
26+
options.page_load_strategy = 'normal'
27+
options.enable_bidi = True
28+
options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
29+
30+
driver = webdriver.Chrome(options)
31+
32+
trio.run(run, driver)

0 commit comments

Comments
 (0)