Skip to content

CDP Mode - Patch 14 #3299

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

Merged
merged 6 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion examples/cdp_mode/ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@

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

🐙 <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!)
🐙 <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.

--------

<!-- 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>
<p>(<b><a href="https://www.youtube.com/watch?v=Mr90iQmNsKM">Watch the CDP Mode tutorial on YouTube! ▶️</a></b>)</p>

--------

👤 <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.)

Expand Down Expand Up @@ -410,6 +417,8 @@ sb.cdp.assert_url(url)
sb.cdp.assert_url_contains(substring)
sb.cdp.assert_text(text, selector="html")
sb.cdp.assert_exact_text(text, selector="html")
sb.cdp.assert_true()
sb.cdp.assert_false()
sb.cdp.scroll_into_view(selector)
sb.cdp.scroll_to_y(y)
sb.cdp.scroll_to_top()
Expand Down
72 changes: 72 additions & 0 deletions examples/cdp_mode/raw_demo_site.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""Example of using various CDP Mode commands"""
from seleniumbase import SB

with SB(uc=True, test=True) as sb:
url = "https://seleniumbase.io/demo_page"
sb.activate_cdp_mode(url)

# Assert various things
sb.cdp.assert_title("Web Testing Page")
sb.cdp.assert_element("tbody#tbodyId")
sb.cdp.assert_text("Demo Page", "h1")

# Type text into various text fields and then assert
sb.cdp.type("#myTextInput", "This is Automated")
sb.cdp.type("textarea.area1", "Testing Time!\n")
sb.cdp.type('[name="preText2"]', "Typing Text!")
sb.cdp.assert_text("This is Automated", "#myTextInput")
sb.cdp.assert_text("Testing Time!\n", "textarea.area1")
sb.cdp.assert_text("Typing Text!", '[name="preText2"]')

# Hover & click a dropdown element and assert results
sb.cdp.assert_text("Automation Practice", "h3")
sb.cdp.gui_hover_and_click("#myDropdown", "#dropOption2")
sb.cdp.assert_text("Link Two Selected", "h3")

# Click a button and then verify the expected results
sb.cdp.assert_text("This Text is Green", "#pText")
sb.cdp.click('button:contains("Click Me")')
sb.cdp.assert_text("This Text is Purple", "#pText")

# Verify that a slider control updates a progress bar
sb.cdp.assert_element('progress[value="50"]')
sb.cdp.set_value("input#mySlider", "100")
sb.cdp.assert_element('progress[value="100"]')

# Verify that a "select" option updates a meter bar
sb.cdp.assert_element('meter[value="0.25"]')
sb.cdp.select_option_by_text("#mySelect", "Set to 75%")
sb.cdp.assert_element('meter[value="0.75"]')

# Verify that clicking a radio button selects it
sb.cdp.assert_false(sb.cdp.is_selected("#radioButton2"))
sb.cdp.click("#radioButton2")
sb.cdp.assert_true(sb.cdp.is_selected("#radioButton2"))

# Verify that clicking a checkbox makes it selected
sb.cdp.assert_element_not_visible("img#logo")
sb.cdp.assert_false(sb.cdp.is_selected("#checkBox1"))
sb.cdp.click("#checkBox1")
sb.cdp.assert_true(sb.cdp.is_selected("#checkBox1"))
sb.cdp.assert_element("img#logo")

# Verify clicking on multiple elements with one call
sb.cdp.assert_false(sb.cdp.is_selected("#checkBox2"))
sb.cdp.assert_false(sb.cdp.is_selected("#checkBox3"))
sb.cdp.assert_false(sb.cdp.is_selected("#checkBox4"))
sb.cdp.click_visible_elements("input.checkBoxClassB")
sb.cdp.assert_true(sb.cdp.is_selected("#checkBox2"))
sb.cdp.assert_true(sb.cdp.is_selected("#checkBox3"))
sb.cdp.assert_true(sb.cdp.is_selected("#checkBox4"))

# Verify Drag and Drop
sb.cdp.assert_element_not_visible("div#drop2 img#logo")
sb.cdp.gui_drag_and_drop("img#logo", "div#drop2")
sb.cdp.assert_element("div#drop2 img#logo")

# Click inside an iframe and test highlighting
sb.cdp.flash("iframe#myFrame3")
sb.cdp.sleep(1)
sb.cdp.nested_click("iframe#myFrame3", ".fBox")
sb.cdp.sleep(0.5)
sb.cdp.highlight("iframe#myFrame3")
1 change: 1 addition & 0 deletions examples/presenter/uc_presentation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# https://www.youtube.com/watch?v=5dMFI3e85ig
import os
import subprocess
from contextlib import suppress
Expand Down
1 change: 1 addition & 0 deletions examples/presenter/uc_presentation_3.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# https://www.youtube.com/watch?v=-EpZlhGWo9k
import sys
from contextlib import suppress
from seleniumbase import BaseCase
Expand Down
Loading