Skip to content

Commit 1139783

Browse files
authored
Merge pull request #4257 from seleniumbase/cdp-mode-patch-95
CDP Mode: Patch 95
2 parents 1ab98f5 + 373a751 commit 1139783

File tree

18 files changed

+110
-23
lines changed

18 files changed

+110
-23
lines changed

.dockerignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.git/
2+
.gitignore
3+
.idea/
4+
.mypy_cache/
5+
.pytest_cache/
6+
.tox/
7+
.venv/
8+
.vscode/
9+
*.egg-info/
10+
*.log
11+
*.py[cod]
12+
__pycache__/
13+
build/
14+
dist/
15+
help_docs/
16+
node_modules/
17+
site/

Dockerfile

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,18 @@ RUN apt-get install -y \
4646
#============================
4747
RUN apt-get update
4848
RUN apt-get install -y \
49+
dbus-x11 \
4950
libatk1.0-0 \
5051
libatspi2.0-0 \
5152
libdbus-1-3 \
5253
libdrm2 \
5354
libgtk-3-0 \
5455
libnspr4 \
56+
libasound2t64 \
5557
libu2f-udev \
5658
libwayland-client0 \
59+
libx11-6 \
60+
libx11-xcb1 \
5761
libxdamage1 \
5862
libxfixes3 \
5963
libxkbcommon0
@@ -62,7 +66,7 @@ RUN apt-get install -y \
6266
# Install useful utilities
6367
#==========================
6468
RUN apt-get update
65-
RUN apt-get install -y xdg-utils ca-certificates
69+
RUN apt-get install -y xdg-utils ca-certificates x11vnc
6670

6771
#=================================
6872
# Install Bash Command Line Tools
@@ -127,13 +131,20 @@ RUN cd /SeleniumBase && ls && pip install -r requirements.txt --upgrade
127131
RUN cd /SeleniumBase && pip install .
128132
RUN pip install pyautogui
129133
RUN pip install playwright
134+
RUN seleniumbase get cft
130135
RUN seleniumbase get chromium
131136

132137
#=======================
133138
# Download chromedriver
134139
#=======================
135140
RUN seleniumbase get chromedriver --path
136141

142+
#==============
143+
# Extra config
144+
#==============
145+
ENV DISPLAY=":99"
146+
RUN Xvfb :99 -screen 1 1920x1080x16 -nolisten tcp &
147+
137148
#==========================================
138149
# Create entrypoint and grab example tests
139150
#==========================================

examples/cdp_mode/ReadMe.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,10 @@ await tab.bring_to_front()
666666
await tab.set_window_state(
667667
left=0, top=0, width=1280, height=720, state="normal")
668668
await tab.get_navigation_history()
669+
await tab.get_user_agent()
670+
await tab.get_cookie_string()
671+
await tab.get_locale_code()
672+
await tab.is_online()
669673
await tab.open_external_inspector() # Open separate browser for debugging
670674
await tab.close()
671675
await tab.scroll_down(amount=25)
@@ -689,6 +693,7 @@ await tab.get_window_rect()
689693
await tab.get_gui_element_rect(selector, timeout=5) # (screen-based)
690694
await tab.get_title()
691695
await tab.get_current_url()
696+
await tab.get_origin()
692697
await tab.send_keys(selector, text, timeout=5)
693698
await tab.type(selector, text, timeout=5)
694699
await tab.click(selector, timeout=5)

examples/cdp_mode/playwright/raw_copilot_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ async def main():
1717
query = "Playwright Python connect_over_cdp() sync example"
1818
await page.fill("textarea#userInput", query)
1919
await page.click('button[data-testid="submit-button"]')
20-
await driver.sleep(3)
20+
await driver.sleep(4)
2121
await driver.solve_captcha()
2222
await page.wait_for_selector('button[data-testid*="-thumbs-up"]')
2323
await driver.sleep(4)

examples/cdp_mode/playwright/raw_copilot_nested.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
query = "Playwright Python connect_over_cdp() sync example"
1616
page.fill("textarea#userInput", query)
1717
page.click('button[data-testid="submit-button"]')
18-
sb.sleep(3)
18+
sb.sleep(4)
1919
sb.solve_captcha()
2020
page.wait_for_selector('button[data-testid*="-thumbs-up"]')
2121
sb.sleep(4)

examples/cdp_mode/playwright/raw_copilot_sync.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
query = "Playwright Python connect_over_cdp() sync example"
1515
page.fill("textarea#userInput", query)
1616
page.click('button[data-testid="submit-button"]')
17-
sb.sleep(3)
17+
sb.sleep(4)
1818
sb.solve_captcha()
1919
page.wait_for_selector('button[data-testid*="-thumbs-up"]')
2020
sb.sleep(4)

examples/cdp_mode/raw_cf_captcha.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
url = "https://www.cloudflare.com/login"
55
sb.activate_cdp_mode(url)
66
sb.wait_for_element('div[data-testid*="challenge-widget"]')
7-
sb.sleep(1.5)
7+
sb.sleep(2)
88
sb.solve_captcha()
99
sb.sleep(3)

examples/cdp_mode/raw_cf_clearance.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ def get_cf_clearance_cookie(sb):
1111

1212
url = "https://gitlab.com/users/sign_in"
1313
sb = sb_cdp.Chrome(url)
14-
sb.sleep(2.5) # Wait for CAPTCHA to load
14+
sb.sleep(3) # Wait for CAPTCHA to load
1515
sb.solve_captcha() # (Only if found)
16-
sb.sleep(2.2) # Wait for CAPTCHA success
16+
sb.sleep(2) # Wait for CAPTCHA success
1717
cf_cookie = get_cf_clearance_cookie(sb)
1818
if cf_cookie:
1919
print("cf_clearance cookie: %s" % cf_cookie.value)

examples/cdp_mode/raw_multi_captcha.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77

88
def main(url):
9-
sb = sb_cdp.Chrome(url, lang="en")
9+
sb = sb_cdp.Chrome(url, lang="en", incognito=True)
1010
sb.set_window_rect(randint(4, 680), randint(8, 380), 840, 520)
1111
sb.sleep(2)
1212
sb.gui_click_captcha()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import asyncio
2+
from seleniumbase import cdp_driver
3+
4+
5+
async def main():
6+
search = "reddit+scraper"
7+
url = f"https://www.reddit.com/r/webscraping/search/?q={search}"
8+
driver = await cdp_driver.start_async(use_chromium=True)
9+
page = await driver.get(url)
10+
await page.solve_captcha() # Might not be needed
11+
post_title = '[data-testid="post-title"]'
12+
await page.select(post_title)
13+
for i in range(8):
14+
await page.scroll_down(25)
15+
await page.sleep(0.2)
16+
posts = await page.select_all(post_title)
17+
print('*** Reddit Posts for "%s":' % search)
18+
for post in posts:
19+
print("* " + post.text)
20+
driver.stop()
21+
22+
if __name__ == "__main__":
23+
loop = asyncio.new_event_loop()
24+
loop.run_until_complete(main())

0 commit comments

Comments
 (0)