Skip to content

Commit 82de835

Browse files
authored
Merge pull request #3487 from seleniumbase/cdp-mode-patch-30
CDP Mode - Patch 30
2 parents 5bde4aa + 61d057f commit 82de835

File tree

10 files changed

+116
-25
lines changed

10 files changed

+116
-25
lines changed

examples/cdp_mode/raw_cdp_methods.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import asyncio
2+
from seleniumbase.core import sb_cdp
3+
from seleniumbase.undetected import cdp_driver
4+
5+
url = "https://seleniumbase.io/demo_page"
6+
loop = asyncio.new_event_loop()
7+
driver = cdp_driver.cdp_util.start_sync()
8+
page = loop.run_until_complete(driver.get(url))
9+
sb = sb_cdp.CDPMethods(loop, page, driver)
10+
11+
sb.press_keys("input", "Text")
12+
sb.highlight("button")
13+
sb.type("textarea", "Here are some words")
14+
sb.click("button")
15+
sb.set_value("input#mySlider", "100")
16+
sb.click_visible_elements("input.checkBoxClassB")
17+
sb.select_option_by_text("#mySelect", "Set to 75%")
18+
sb.gui_hover_and_click("#myDropdown", "#dropOption2")
19+
sb.gui_click_element("#checkBox1")
20+
sb.gui_drag_and_drop("img#logo", "div#drop2")
21+
sb.nested_click("iframe#myFrame3", ".fBox")
22+
sb.sleep(2)

examples/cdp_mode/raw_cdp_nike.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import asyncio
2+
from seleniumbase.core import sb_cdp
3+
from seleniumbase.undetected import cdp_driver
4+
5+
url = "https://www.nike.com/"
6+
loop = asyncio.new_event_loop()
7+
driver = cdp_driver.cdp_util.start_sync()
8+
page = loop.run_until_complete(driver.get(url))
9+
sb = sb_cdp.CDPMethods(loop, page, driver)
10+
11+
search = "Nike Fly Shoes"
12+
sb.click('div[data-testid="user-tools-container"]')
13+
sb.sleep(1)
14+
sb.press_keys('input[type="search"]', search)
15+
sb.sleep(4)
16+
17+
elements = sb.select_all('ul[data-testid*="products"] figure .details')
18+
if elements:
19+
print('**** Found results for "%s": ****' % search)
20+
for element in elements:
21+
print("* " + element.text)

examples/cdp_mode/raw_driver.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import atexit
2+
from seleniumbase import Driver
3+
4+
driver = Driver(uc=True)
5+
atexit.register(driver.quit)
6+
url = "www.planetminecraft.com/account"
7+
driver.uc_activate_cdp_mode(url)
8+
driver.sleep(1)
9+
driver.uc_gui_click_captcha()
10+
driver.sleep(2)

examples/raw_games.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
"""SB Manager using UC Mode for evading bot-detection."""
22
from seleniumbase import SB
33

4-
with SB(uc=True, test=True, disable_csp=True) as sb:
4+
with SB(uc=True, test=True) as sb:
55
url = "https://steamdb.info/"
66
sb.uc_open_with_reconnect(url, 3)
77
sb.uc_click("a.header-login span", 3)
88
sb.uc_gui_click_captcha()
9-
sb.assert_text("Sign in", "button#js-sign-in", timeout=3)
9+
sb.assert_text("Sign in", "button#js-sign-in")
1010
sb.uc_click("button#js-sign-in", 2)
1111
sb.highlight("div.page_content form")
1212
sb.highlight('button:contains("Sign in")', scroll=False)
13-
sb.set_messenger_theme(location="top_center")
14-
sb.post_message("SeleniumBase wasn't detected", duration=4)
13+
sb.sleep(1)

mkdocs_build/requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pathspec==0.12.1
1414
Babel==2.17.0
1515
paginate==0.5.7
1616
mkdocs==1.6.1
17-
mkdocs-material==9.6.2
17+
mkdocs-material==9.6.3
1818
mkdocs-exclude-search==0.6.6
1919
mkdocs-simple-hooks==0.1.5
2020
mkdocs-material-extensions==1.3.1

seleniumbase/__version__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# seleniumbase package
2-
__version__ = "4.34.8"
2+
__version__ = "4.34.9"

seleniumbase/core/browser_launcher.py

+21
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,10 @@ def _uc_gui_click_captcha(
12511251
)
12521252
):
12531253
frame = '[data-testid*="challenge-"] div'
1254+
elif driver.is_element_present(
1255+
"form.turnstile div#turnstile-widget div:not([class])"
1256+
):
1257+
frame = "form.turnstile #turnstile-widget div:not([class])"
12541258
elif driver.is_element_present(
12551259
'form div:not([class]):has(input[name*="cf-turn"])'
12561260
):
@@ -1291,6 +1295,23 @@ def _uc_gui_click_captcha(
12911295
driver.cdp.evaluate(script)
12921296
else:
12931297
driver.execute_script(script)
1298+
elif (
1299+
driver.is_element_present("form")
1300+
and driver.is_element_present(
1301+
"form.turnstile #turnstile-widget > div:not([class])"
1302+
)
1303+
):
1304+
script = (
1305+
"""var $elements = document.querySelectorAll(
1306+
'form.turnstile #turnstile-widget');
1307+
var index = 0, length = $elements.length;
1308+
for(; index < length; index++){
1309+
$elements[index].setAttribute('align', 'left');}"""
1310+
)
1311+
if __is_cdp_swap_needed(driver):
1312+
driver.cdp.evaluate(script)
1313+
else:
1314+
driver.execute_script(script)
12941315
if not is_in_frame or needs_switch:
12951316
# Currently not in frame (or nested frame outside CF one)
12961317
try:

seleniumbase/fixtures/base_case.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -3256,7 +3256,7 @@ def load_html_string(self, html_string, new_page=True):
32563256
html_string = "\n".join(new_lines)
32573257
soup = self.get_beautiful_soup(html_string)
32583258
found_base = False
3259-
links = soup.findAll("link")
3259+
links = soup.find_all("link")
32603260
href = None
32613261
for link in links:
32623262
if link.get("rel") == ["canonical"] and link.get("href"):
@@ -3272,15 +3272,15 @@ def load_html_string(self, html_string, new_page=True):
32723272
"<head>", '<head><base href="%s">' % href
32733273
)
32743274
elif not found_base:
3275-
bases = soup.findAll("base")
3275+
bases = soup.find_all("base")
32763276
for base in bases:
32773277
if base.get("href"):
32783278
href = base.get("href")
32793279
if href:
32803280
html_string = html_string.replace('base: "."', 'base: "%s"' % href)
32813281

32823282
soup = self.get_beautiful_soup(html_string)
3283-
scripts = soup.findAll("script")
3283+
scripts = soup.find_all("script")
32843284
for script in scripts:
32853285
if script.get("type") != "application/json":
32863286
html_string = html_string.replace(str(script), "")

seleniumbase/undetected/__init__.py

+24-14
Original file line numberDiff line numberDiff line change
@@ -452,14 +452,19 @@ def reconnect(self, timeout=0.1):
452452
self.service.start()
453453
with suppress(Exception):
454454
self.start_session()
455+
time.sleep(0.0075)
455456
with suppress(Exception):
456-
if self.current_url.startswith("chrome-extension://"):
457-
self.close()
458-
if self.service.is_connectable():
459-
self.stop_client()
460-
self.service.stop()
461-
self.service.start()
462-
self.start_session()
457+
for i in range(5):
458+
if self.current_url.startswith("chrome-extension://"):
459+
self.close()
460+
if self.service.is_connectable():
461+
self.stop_client()
462+
self.service.stop()
463+
self.service.start()
464+
self.start_session()
465+
time.sleep(0.0075)
466+
else:
467+
break
463468
self._is_connected = True
464469

465470
def disconnect(self):
@@ -480,14 +485,19 @@ def connect(self):
480485
self.service.start()
481486
with suppress(Exception):
482487
self.start_session()
488+
time.sleep(0.0075)
483489
with suppress(Exception):
484-
if self.current_url.startswith("chrome-extension://"):
485-
self.close()
486-
if self.service.is_connectable():
487-
self.stop_client()
488-
self.service.stop()
489-
self.service.start()
490-
self.start_session()
490+
for i in range(5):
491+
if self.current_url.startswith("chrome-extension://"):
492+
self.close()
493+
if self.service.is_connectable():
494+
self.stop_client()
495+
self.service.stop()
496+
self.service.start()
497+
self.start_session()
498+
time.sleep(0.0075)
499+
else:
500+
break
491501
self._is_connected = True
492502

493503
def start_session(self, capabilities=None):

seleniumbase/undetected/cdp_driver/cdp_util.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,11 @@ async def start_async(*args, **kwargs) -> Browser:
226226
binary_location = None
227227
if "browser_executable_path" in kwargs:
228228
binary_location = kwargs["browser_executable_path"]
229-
if shared_utils.is_chrome_130_or_newer(binary_location):
229+
if (
230+
shared_utils.is_chrome_130_or_newer(binary_location)
231+
and "user_data_dir" in kwargs
232+
and kwargs["user_data_dir"]
233+
):
230234
if "headless" in kwargs:
231235
headless = kwargs["headless"]
232236
decoy_args = kwargs
@@ -257,7 +261,11 @@ def start_sync(*args, **kwargs) -> Browser:
257261
binary_location = None
258262
if "browser_executable_path" in kwargs:
259263
binary_location = kwargs["browser_executable_path"]
260-
if shared_utils.is_chrome_130_or_newer(binary_location):
264+
if (
265+
shared_utils.is_chrome_130_or_newer(binary_location)
266+
and "user_data_dir" in kwargs
267+
and kwargs["user_data_dir"]
268+
):
261269
if "headless" in kwargs:
262270
headless = kwargs["headless"]
263271
decoy_args = kwargs

0 commit comments

Comments
 (0)