Skip to content

Commit 533619d

Browse files
authored
Merge pull request #3232 from seleniumbase/cdp-mode-patch-4
CDP Mode - Patch 4
2 parents dbe6fbb + 6c2d6e3 commit 533619d

File tree

17 files changed

+114
-65
lines changed

17 files changed

+114
-65
lines changed

README.md

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262

6363
👤 Note that <span translate="no">SeleniumBase</span> <a translate="no" href="https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/uc_mode.md"><b>UC Mode</b> (Stealth Mode) has its own ReadMe</a>.
6464

65+
🐙 Also note that Seleniumbase <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/cdp_mode/ReadMe.md"><b>CDP Mode</b> has its own separate ReadMe</a>.
66+
6567
ℹ️ Scripts can be called via <code translate="no"><b>python</b></code>, although some <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/help_docs/syntax_formats.md">Syntax Formats</a> expect <a href="https://docs.pytest.org/en/latest/how-to/usage.html" translate="no"><b>pytest</b></a> (a Python unit-testing framework included with SeleniumBase that can discover, collect, and run tests automatically).
6668

6769
<p align="left">📗 Here's <a href="https://github.com/seleniumbase/SeleniumBase/blob/master/examples/my_first_test.py">my_first_test.py</a>, which tests login, shopping, and checkout:</p>
@@ -315,48 +317,49 @@ pip install -e .
315317
🔵 **Type ``seleniumbase`` or ``sbase`` to verify that SeleniumBase was installed successfully:**
316318

317319
```bash
318-
______ __ _ ____
319-
/ ____/__ / /__ ____ (_)_ ______ ___ / _ \____ ________
320-
\__ \/ _ \/ / _ \/ __ \/ / / / / __ `__ \ / /_) / __ \/ ___/ _ \
321-
___/ / __/ / __/ / / / / /_/ / / / / / // /_) / (_/ /__ / __/
322-
/____/\___/_/\___/_/ /_/_/\__,_/_/ /_/ /_//_____/\__,_/____/\___/
323-
------------------------------------------------------------------
324-
325-
* USAGE: "seleniumbase [COMMAND] [PARAMETERS]"
326-
* OR: "sbase [COMMAND] [PARAMETERS]"
327-
328-
COMMANDS:
329-
get / install [DRIVER] [OPTIONS]
330-
methods (List common Python methods)
331-
options (List common pytest options)
332-
behave-options (List common behave options)
333-
gui / commander [OPTIONAL PATH or TEST FILE]
334-
behave-gui (SBase Commander for Behave)
335-
caseplans [OPTIONAL PATH or TEST FILE]
336-
mkdir [DIRECTORY] [OPTIONS]
337-
mkfile [FILE.py] [OPTIONS]
338-
mkrec / codegen [FILE.py] [OPTIONS]
339-
recorder (Open Recorder Desktop App.)
340-
record (If args: mkrec. Else: App.)
341-
mkpres [FILE.py] [LANG]
342-
mkchart [FILE.py] [LANG]
343-
print [FILE] [OPTIONS]
344-
translate [SB_FILE.py] [LANG] [ACTION]
345-
convert [WEBDRIVER_UNITTEST_FILE.py]
346-
extract-objects [SB_FILE.py]
347-
inject-objects [SB_FILE.py] [OPTIONS]
348-
objectify [SB_FILE.py] [OPTIONS]
349-
revert-objects [SB_FILE.py] [OPTIONS]
350-
encrypt / obfuscate
351-
decrypt / unobfuscate
352-
download server (Get Selenium Grid JAR file)
353-
grid-hub [start|stop] [OPTIONS]
354-
grid-node [start|stop] --hub=[HOST/IP]
355-
* (EXAMPLE: "sbase get chromedriver latest") *
356-
357-
Type "sbase help [COMMAND]" for specific command info.
358-
For info on all commands, type: "seleniumbase --help".
359-
Use "pytest" for running tests.
320+
___ _ _ ___
321+
/ __| ___| |___ _ _ (_)_ _ _ __ | _ ) __ _ ______
322+
\__ \/ -_) / -_) ' \| | \| | ' \ | _ \/ _` (_-< -_)
323+
|___/\___|_\___|_||_|_|\_,_|_|_|_\|___/\__,_/__|___|
324+
----------------------------------------------------
325+
326+
╭──────────────────────────────────────────────────╮
327+
* USAGE: "seleniumbase [COMMAND] [PARAMETERS]"
328+
* OR: "sbase [COMMAND] [PARAMETERS]"
329+
│ │
330+
│ COMMANDS: PARAMETERS / DESCRIPTIONS: │
331+
│ get / install [DRIVER_NAME] [OPTIONS] │
332+
│ methods (List common Python methods) │
333+
│ options (List common pytest options) │
334+
│ behave-options (List common behave options) │
335+
│ gui / commander [OPTIONAL PATH or TEST FILE] │
336+
│ behave-gui (SBase Commander for Behave) │
337+
│ caseplans [OPTIONAL PATH or TEST FILE] │
338+
│ mkdir [DIRECTORY] [OPTIONS] │
339+
│ mkfile [FILE.py] [OPTIONS] │
340+
│ mkrec / codegen [FILE.py] [OPTIONS] │
341+
│ recorder (Open Recorder Desktop App.) │
342+
│ record (If args: mkrec. Else: App.) │
343+
│ mkpres [FILE.py] [LANG] │
344+
│ mkchart [FILE.py] [LANG] │
345+
│ print [FILE] [OPTIONS] │
346+
│ translate [SB_FILE.py] [LANG] [ACTION] │
347+
│ convert [WEBDRIVER_UNITTEST_FILE.py] │
348+
│ extract-objects [SB_FILE.py] │
349+
│ inject-objects [SB_FILE.py] [OPTIONS] │
350+
│ objectify [SB_FILE.py] [OPTIONS] │
351+
│ revert-objects [SB_FILE.py] [OPTIONS] │
352+
│ encrypt / obfuscate │
353+
│ decrypt / unobfuscate │
354+
│ proxy (Start a basic proxy server) │
355+
│ download server (Get Selenium Grid JAR file) │
356+
│ grid-hub [start|stop] [OPTIONS] │
357+
│ grid-node [start|stop] --hub=[HOST/IP] │
358+
│ │
359+
* EXAMPLE => "sbase get chromedriver stable"
360+
* For command info => "sbase help [COMMAND]"
361+
* For info on all commands => "sbase --help"
362+
╰──────────────────────────────────────────────────╯
360363
```
361364

362365
<h3>🔵 Downloading webdrivers:</h3>
@@ -1375,7 +1378,6 @@ pytest --reruns=1 --reruns-delay=1
13751378
<span><a href="https://github.com/seleniumbase/SeleniumBase"><img src="https://seleniumbase.github.io/img/social/share_github.svg" title="SeleniumBase on GitHub" alt="SeleniumBase on GitHub" width="64" /></a></span>
13761379
<span><a href="https://discord.gg/EdhQTn3EyE"><img src="https://seleniumbase.github.io/other/discord_icon.png" title="SeleniumBase on Discord" alt="SeleniumBase on Discord" width="66" /></a></span>
13771380
<span><a href="https://www.facebook.com/SeleniumBase"><img src="https://seleniumbase.io/img/social/share_facebook.svg" title="SeleniumBase on Facebook" alt="SeleniumBase on Facebook" width="62" /></a></span>
1378-
<span><a href="https://gitter.im/seleniumbase/SeleniumBase" target="_blank"><img src="https://seleniumbase.github.io/img/social/share_gitter.svg" title="SeleniumBase on Gitter" alt="SeleniumBase on Gitter" width="48" /></a></span>
13791381
</div></p>
13801382
13811383
<p><div><b><a href="https://github.com/mdmintz">https://github.com/mdmintz</a></b></div></p>

examples/cdp_mode/raw_antibot.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from seleniumbase import SB
2+
3+
with SB(uc=True, test=True) as sb:
4+
url = "https://seleniumbase.io/antibot/login"
5+
sb.activate_cdp_mode(url)
6+
sb.press_keys("input#username", "demo_user")
7+
sb.press_keys("input#password", "secret_pass")
8+
x, y = sb.cdp.get_gui_element_center("button#myButton")
9+
sb.uc_gui_click_x_y(x, y)
10+
sb.sleep(1.5)
11+
x, y = sb.cdp.get_gui_element_center("a#log-in")
12+
sb.uc_gui_click_x_y(x, y)
13+
sb.assert_text("Welcome!", "h1")
14+
sb.set_messenger_theme(location="bottom_center")
15+
sb.post_message("SeleniumBase wasn't detected!")
16+
sb.sleep(1.5)

examples/cdp_mode/raw_gitlab.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from seleniumbase import SB
2+
3+
with SB(uc=True, test=True, locale_code="en") as sb:
4+
url = "https://gitlab.com/users/sign_in"
5+
sb.activate_cdp_mode(url)
6+
sb.uc_gui_click_captcha()
7+
sb.assert_text("Username", '[for="user_login"]', timeout=3)
8+
sb.assert_element('label[for="user_login"]')
9+
sb.highlight('button:contains("Sign in")')
10+
sb.highlight('h1:contains("GitLab.com")')
11+
sb.post_message("SeleniumBase wasn't detected", duration=4)

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ colorama>=0.4.6
2121
pyyaml>=6.0.2
2222
pygments>=2.18.0
2323
pyreadline3>=3.5.3;platform_system=="Windows"
24-
tabcompleter>=1.3.3
25-
pdbp>=1.5.4
24+
tabcompleter>=1.4.0
25+
pdbp>=1.6.0
2626
idna==3.10
2727
chardet==5.2.0
2828
charset-normalizer==3.4.0

seleniumbase/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# seleniumbase package
2-
__version__ = "4.32.3"
2+
__version__ = "4.32.4"

seleniumbase/core/browser_launcher.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ def uc_open_with_cdp_mode(driver, url=None):
542542
driver.cdp_base = loop.run_until_complete(
543543
cdp_util.start(host=cdp_host, port=cdp_port)
544544
)
545+
545546
page = loop.run_until_complete(driver.cdp_base.get(url))
546547
loop.run_until_complete(page.activate())
547548
if not safe_url:

seleniumbase/fixtures/base_case.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4167,13 +4167,13 @@ def get_new_driver(
41674167
device_pixel_ratio=d_p_r,
41684168
browser=browser_name,
41694169
)
4170-
time.sleep(0.2)
4170+
time.sleep(0.16)
41714171
except Exception:
41724172
pass
41734173
finally:
41744174
with suppress(Exception):
41754175
decoy_driver.quit()
4176-
time.sleep(0.1)
4176+
time.sleep(0.08)
41774177
# Launch a web browser
41784178
new_driver = browser_launcher.get_driver(
41794179
browser_name=browser_name,

seleniumbase/plugins/driver_manager.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -870,13 +870,13 @@ def Driver(
870870
device_pixel_ratio=d_p_r,
871871
browser=browser_name,
872872
)
873-
time.sleep(0.2)
873+
time.sleep(0.16)
874874
except Exception:
875875
pass
876876
finally:
877877
with suppress(Exception):
878878
decoy_driver.quit()
879-
time.sleep(0.1)
879+
time.sleep(0.08)
880880

881881
driver = browser_launcher.get_driver(
882882
browser_name=browser_name,

seleniumbase/undetected/__init__.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python3
21
import logging
32
import os
43
import re
@@ -433,10 +432,15 @@ def reconnect(self, timeout=0.1):
433432
time.sleep(timeout)
434433
with suppress(Exception):
435434
self.service.start()
436-
time.sleep(0.012)
437435
with suppress(Exception):
438436
self.start_session()
439-
time.sleep(0.012)
437+
with suppress(Exception):
438+
if self.current_url.startswith("chrome-extension://"):
439+
self.close()
440+
self.service.stop()
441+
self.service.start()
442+
self.start_session()
443+
self._is_connected = True
440444

441445
def disconnect(self):
442446
"""Stops the chromedriver service that runs in the background.
@@ -445,19 +449,22 @@ def disconnect(self):
445449
with suppress(Exception):
446450
self.service.stop()
447451
self._is_connected = False
448-
time.sleep(0.012)
449452

450453
def connect(self):
451454
"""Starts the chromedriver service that runs in the background
452455
and recreates the session."""
453456
if hasattr(self, "service"):
454457
with suppress(Exception):
455458
self.service.start()
456-
time.sleep(0.012)
457459
with suppress(Exception):
458460
self.start_session()
461+
with suppress(Exception):
462+
if self.current_url.startswith("chrome-extension://"):
463+
self.close()
464+
self.service.stop()
465+
self.service.start()
466+
self.start_session()
459467
self._is_connected = True
460-
time.sleep(0.012)
461468

462469
def start_session(self, capabilities=None):
463470
if not capabilities:

seleniumbase/undetected/cdp.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python3
21
import fasteners
32
import json
43
import logging

seleniumbase/undetected/cdp_driver/config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sys
66
import tempfile
77
import zipfile
8+
from seleniumbase.config import settings
89
from typing import Union, List, Optional
910

1011
__all__ = [
@@ -101,7 +102,13 @@ def __init__(
101102
# Other keyword args will be accessible by attribute
102103
self.__dict__.update(kwargs)
103104
super().__init__()
105+
start_width = settings.CHROME_START_WIDTH
106+
start_height = settings.CHROME_START_HEIGHT
107+
start_x = settings.WINDOW_START_X
108+
start_y = settings.WINDOW_START_Y
104109
self._default_browser_args = [
110+
"--window-size=%s,%s" % (start_width, start_height),
111+
"--window-position=%s,%s" % (start_x, start_y),
105112
"--remote-allow-origins=*",
106113
"--no-first-run",
107114
"--no-service-autorun",

seleniumbase/undetected/cdp_driver/connection.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,14 @@ async def aclose(self):
292292
if self.listener and self.listener.running:
293293
self.listener.cancel()
294294
self.enabled_domains.clear()
295-
await self.websocket.close()
295+
await asyncio.sleep(0.015)
296+
try:
297+
await self.websocket.close()
298+
except Exception:
299+
logger.debug(
300+
"\n❌ Error closing websocket connection to %s",
301+
self.websocket_url
302+
)
296303
logger.debug(
297304
"\n❌ Closed websocket connection to %s", self.websocket_url
298305
)
@@ -540,6 +547,7 @@ async def listener_loop(self):
540547
self.idle.set()
541548
# Pause for a moment.
542549
# await asyncio.sleep(self.time_before_considered_idle / 10)
550+
await asyncio.sleep(0.015)
543551
continue
544552
except (Exception,) as e:
545553
logger.debug(

seleniumbase/undetected/dprocess.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python3
21
import os
32
import sys
43
import atexit

seleniumbase/undetected/options.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python3
21
import json
32
import os
43
from contextlib import suppress
@@ -59,7 +58,9 @@ def handle_prefs(self, user_data_dir):
5958
json.load(f), undot_prefs
6059
)
6160
with suppress(Exception):
62-
with open(prefs_file, encoding="utf-8", mode="w") as f:
61+
with open(
62+
prefs_file, encoding="utf-8", mode="w", errors="ignore"
63+
) as f:
6364
json.dump(undot_prefs, f)
6465
# Remove experimental_options to avoid errors
6566
del self._experimental_options["prefs"]

seleniumbase/undetected/patcher.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python3
21
import io
32
import logging
43
import os

seleniumbase/undetected/reactor.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python3
21
import asyncio
32
import json
43
import logging

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@
170170
'pyyaml>=6.0.2',
171171
'pygments>=2.18.0',
172172
'pyreadline3>=3.5.3;platform_system=="Windows"',
173-
"tabcompleter>=1.3.3",
174-
"pdbp>=1.5.4",
173+
"tabcompleter>=1.4.0",
174+
"pdbp>=1.6.0",
175175
"idna==3.10",
176176
'chardet==5.2.0',
177177
'charset-normalizer==3.4.0',

0 commit comments

Comments
 (0)