Skip to content

Commit c81ab62

Browse files
authored
Merge pull request #3299 from seleniumbase/cdp-mode-patch-14
CDP Mode - Patch 14
2 parents 30caa81 + 266fab8 commit c81ab62

File tree

11 files changed

+1195
-19
lines changed

11 files changed

+1195
-19
lines changed

examples/cdp_mode/ReadMe.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22

33
## [<img src="https://seleniumbase.github.io/img/logo6.png" title="SeleniumBase" width="32">](https://github.com/seleniumbase/SeleniumBase/) CDP Mode 🐙
44

5-
🐙 <b translate="no">SeleniumBase</b> <b translate="no">CDP Mode</b> (Chrome Devtools Protocol Mode) is a special mode inside of <b><a href="https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/uc_mode.md" translate="no"><span translate="no">SeleniumBase UC Mode</span></a></b> that lets bots appear human while controlling the browser with the <b translate="no">CDP-Driver</b>. Although regular <span translate="no">UC Mode</span> can't perform <span translate="no">WebDriver</span> actions while the <code>driver</code> is disconnected from the browser, the <span translate="no">CDP-Driver</span> can still perform actions while maintaining its cover. (For Python 3.11 or newer!)
5+
🐙 <b translate="no">SeleniumBase</b> <b translate="no">CDP Mode</b> (Chrome Devtools Protocol Mode) is a special mode inside of <b><a href="https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/uc_mode.md" translate="no"><span translate="no">SeleniumBase UC Mode</span></a></b> that lets bots appear human while controlling the browser with the <b translate="no">CDP-Driver</b>. Although regular <span translate="no">UC Mode</span> can't perform <span translate="no">WebDriver</span> actions while the <code>driver</code> is disconnected from the browser, the <span translate="no">CDP-Driver</span> can still perform actions while maintaining its cover.
6+
7+
--------
8+
9+
<!-- YouTube View --><a href="https://www.youtube.com/watch?v=Mr90iQmNsKM"><img src="http://img.youtube.com/vi/Mr90iQmNsKM/0.jpg" title="SeleniumBase on YouTube" width="366" /></a>
10+
<p>(<b><a href="https://www.youtube.com/watch?v=Mr90iQmNsKM">Watch the CDP Mode tutorial on YouTube! ▶️</a></b>)</p>
11+
12+
--------
613

714
👤 <b translate="no">UC Mode</b> avoids bot-detection by first disconnecting WebDriver from the browser at strategic times, calling special <code>PyAutoGUI</code> methods to bypass CAPTCHAs (as needed), and finally reconnecting the <code>driver</code> afterwards so that WebDriver actions can be performed again. Although this approach works for bypassing simple CAPTCHAs, more flexibility is needed for bypassing bot-detection on websites with advanced protection. (That's where <b translate="no">CDP Mode</b> comes in.)
815

@@ -410,6 +417,8 @@ sb.cdp.assert_url(url)
410417
sb.cdp.assert_url_contains(substring)
411418
sb.cdp.assert_text(text, selector="html")
412419
sb.cdp.assert_exact_text(text, selector="html")
420+
sb.cdp.assert_true()
421+
sb.cdp.assert_false()
413422
sb.cdp.scroll_into_view(selector)
414423
sb.cdp.scroll_to_y(y)
415424
sb.cdp.scroll_to_top()

examples/cdp_mode/raw_demo_site.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
"""Example of using various CDP Mode commands"""
2+
from seleniumbase import SB
3+
4+
with SB(uc=True, test=True) as sb:
5+
url = "https://seleniumbase.io/demo_page"
6+
sb.activate_cdp_mode(url)
7+
8+
# Assert various things
9+
sb.cdp.assert_title("Web Testing Page")
10+
sb.cdp.assert_element("tbody#tbodyId")
11+
sb.cdp.assert_text("Demo Page", "h1")
12+
13+
# Type text into various text fields and then assert
14+
sb.cdp.type("#myTextInput", "This is Automated")
15+
sb.cdp.type("textarea.area1", "Testing Time!\n")
16+
sb.cdp.type('[name="preText2"]', "Typing Text!")
17+
sb.cdp.assert_text("This is Automated", "#myTextInput")
18+
sb.cdp.assert_text("Testing Time!\n", "textarea.area1")
19+
sb.cdp.assert_text("Typing Text!", '[name="preText2"]')
20+
21+
# Hover & click a dropdown element and assert results
22+
sb.cdp.assert_text("Automation Practice", "h3")
23+
sb.cdp.gui_hover_and_click("#myDropdown", "#dropOption2")
24+
sb.cdp.assert_text("Link Two Selected", "h3")
25+
26+
# Click a button and then verify the expected results
27+
sb.cdp.assert_text("This Text is Green", "#pText")
28+
sb.cdp.click('button:contains("Click Me")')
29+
sb.cdp.assert_text("This Text is Purple", "#pText")
30+
31+
# Verify that a slider control updates a progress bar
32+
sb.cdp.assert_element('progress[value="50"]')
33+
sb.cdp.set_value("input#mySlider", "100")
34+
sb.cdp.assert_element('progress[value="100"]')
35+
36+
# Verify that a "select" option updates a meter bar
37+
sb.cdp.assert_element('meter[value="0.25"]')
38+
sb.cdp.select_option_by_text("#mySelect", "Set to 75%")
39+
sb.cdp.assert_element('meter[value="0.75"]')
40+
41+
# Verify that clicking a radio button selects it
42+
sb.cdp.assert_false(sb.cdp.is_selected("#radioButton2"))
43+
sb.cdp.click("#radioButton2")
44+
sb.cdp.assert_true(sb.cdp.is_selected("#radioButton2"))
45+
46+
# Verify that clicking a checkbox makes it selected
47+
sb.cdp.assert_element_not_visible("img#logo")
48+
sb.cdp.assert_false(sb.cdp.is_selected("#checkBox1"))
49+
sb.cdp.click("#checkBox1")
50+
sb.cdp.assert_true(sb.cdp.is_selected("#checkBox1"))
51+
sb.cdp.assert_element("img#logo")
52+
53+
# Verify clicking on multiple elements with one call
54+
sb.cdp.assert_false(sb.cdp.is_selected("#checkBox2"))
55+
sb.cdp.assert_false(sb.cdp.is_selected("#checkBox3"))
56+
sb.cdp.assert_false(sb.cdp.is_selected("#checkBox4"))
57+
sb.cdp.click_visible_elements("input.checkBoxClassB")
58+
sb.cdp.assert_true(sb.cdp.is_selected("#checkBox2"))
59+
sb.cdp.assert_true(sb.cdp.is_selected("#checkBox3"))
60+
sb.cdp.assert_true(sb.cdp.is_selected("#checkBox4"))
61+
62+
# Verify Drag and Drop
63+
sb.cdp.assert_element_not_visible("div#drop2 img#logo")
64+
sb.cdp.gui_drag_and_drop("img#logo", "div#drop2")
65+
sb.cdp.assert_element("div#drop2 img#logo")
66+
67+
# Click inside an iframe and test highlighting
68+
sb.cdp.flash("iframe#myFrame3")
69+
sb.cdp.sleep(1)
70+
sb.cdp.nested_click("iframe#myFrame3", ".fBox")
71+
sb.cdp.sleep(0.5)
72+
sb.cdp.highlight("iframe#myFrame3")

examples/presenter/uc_presentation.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# https://www.youtube.com/watch?v=5dMFI3e85ig
12
import os
23
import subprocess
34
from contextlib import suppress

examples/presenter/uc_presentation_3.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# https://www.youtube.com/watch?v=-EpZlhGWo9k
12
import sys
23
from contextlib import suppress
34
from seleniumbase import BaseCase

0 commit comments

Comments
 (0)