Skip to content

Commit 992212c

Browse files
authored
Merge pull request #3331 from seleniumbase/cdp-mode-patch-18
CDP Mode - Patch 18
2 parents b7c49e1 + 78d86f5 commit 992212c

13 files changed

+59
-33
lines changed

examples/cdp_mode/ReadMe.md

+2
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ sb.cdp.get_document()
384384
sb.cdp.get_flattened_document()
385385
sb.cdp.get_element_attributes(selector)
386386
sb.cdp.get_element_attribute(selector, attribute)
387+
sb.cdp.get_attribute(selector, attribute)
387388
sb.cdp.get_element_html(selector)
388389
sb.cdp.set_locale(locale)
389390
sb.cdp.set_attributes(selector, attribute, value)
@@ -463,6 +464,7 @@ element.type(text)
463464
element.get_position()
464465
element.get_html()
465466
element.get_js_attributes()
467+
element.get_attribute(attribute)
466468
```
467469

468470
--------

examples/cdp_mode/raw_fingerprint.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from seleniumbase import SB
22

3-
with SB(uc=True, test=True, incognito=True) as sb:
3+
with SB(uc=True, test=True) as sb:
44
url = "https://demo.fingerprint.com/playground"
55
sb.activate_cdp_mode(url)
66
sb.sleep(1)

examples/cdp_mode/raw_publication.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from seleniumbase import SB
2+
3+
with SB(uc=True, test=True, locale_code="en", ad_block=True) as sb:
4+
url = "https://www.researchgate.net/search/publication"
5+
sb.activate_cdp_mode(url)
6+
sb.sleep(2)
7+
if sb.cdp.is_element_visible("p.spacer-bottom"):
8+
sb.uc_gui_click_captcha()
9+
shadow_head = "div.main-content div"
10+
if sb.is_element_present(shadow_head):
11+
sb.cdp.gui_click_element(shadow_head)
12+
sb.sleep(1.5)
13+
sb.cdp.remove_elements('div[class*="ad-container"]')
14+
sb.cdp.remove_elements("div.lite-page-ad")
15+
sb.sleep(0.5)
16+
sb.assert_text("Discover the world's scientific knowledge")
17+
sb.highlight('h1[class*="nova"]')
18+
sb.highlight('input[name="q"]')
19+
sb.highlight("a.menu-item.selected")
20+
sb.sleep(1)

examples/cdp_mode/raw_res_sb.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Using CDP.network.ResponseReceived and CDP.network.RequestWillBeSent."""
1+
"""Using CDP.network.RequestWillBeSent and CDP.network.ResponseReceived."""
22
import colorama
33
import mycdp
44
import sys

examples/raw_recaptcha.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
from seleniumbase import SB
22

3-
with SB(uc=True, test=True) as sb:
3+
with SB(uc=True, test=True, incognito=True) as sb:
44
url = "https://seleniumbase.io/apps/recaptcha"
55
sb.activate_cdp_mode(url)
6+
sb.sleep(1)
67
sb.uc_gui_handle_captcha() # Try with TAB + SPACEBAR
78
sb.assert_element("img#captcha-success", timeout=3)
89
sb.set_messenger_theme(location="top_left")
910
sb.post_message("SeleniumBase wasn't detected", duration=3)
1011

11-
with SB(uc=True, test=True) as sb:
12+
with SB(uc=True, test=True, incognito=True) as sb:
1213
url = "https://seleniumbase.io/apps/recaptcha"
1314
sb.activate_cdp_mode(url)
15+
sb.sleep(1)
1416
sb.uc_gui_click_captcha('iframe[src*="/recaptcha/"]')
1517
sb.assert_element("img#captcha-success", timeout=3)
1618
sb.set_messenger_theme(location="top_left")

mkdocs_build/requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pymdown-extensions>=10.12
66
pipdeptree>=2.24.0
77
python-dateutil>=2.8.2
88
Markdown==3.7
9-
markdown2==2.5.1
9+
markdown2==2.5.2
1010
MarkupSafe==3.0.2
1111
Jinja2==3.1.4
1212
click==8.1.7

seleniumbase/__version__.py

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

seleniumbase/core/browser_launcher.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,7 @@ def uc_open_with_cdp_mode(driver, url=None):
658658
cdp.get_window = CDPM.get_window
659659
cdp.get_element_attributes = CDPM.get_element_attributes
660660
cdp.get_element_attribute = CDPM.get_element_attribute
661+
cdp.get_attribute = CDPM.get_attribute
661662
cdp.get_element_html = CDPM.get_element_html
662663
cdp.get_element_rect = CDPM.get_element_rect
663664
cdp.get_element_size = CDPM.get_element_size
@@ -2321,7 +2322,6 @@ def _set_chrome_options(
23212322
chrome_options.add_argument("--disable-ipc-flooding-protection")
23222323
chrome_options.add_argument("--disable-password-generation")
23232324
chrome_options.add_argument("--disable-domain-reliability")
2324-
chrome_options.add_argument("--disable-component-update")
23252325
chrome_options.add_argument("--disable-breakpad")
23262326
included_disabled_features = []
23272327
included_disabled_features.append("OptimizationHints")
@@ -4071,7 +4071,6 @@ def get_local_driver(
40714071
edge_options.add_argument("--disable-ipc-flooding-protection")
40724072
edge_options.add_argument("--disable-password-generation")
40734073
edge_options.add_argument("--disable-domain-reliability")
4074-
edge_options.add_argument("--disable-component-update")
40754074
edge_options.add_argument("--disable-breakpad")
40764075
included_disabled_features = []
40774076
included_disabled_features.append("OptimizationHints")

seleniumbase/core/sb_cdp.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ def __add_sync_methods(self, element):
8383
element.get_position = lambda: self.__get_position(element)
8484
element.get_html = lambda: self.__get_html(element)
8585
element.get_js_attributes = lambda: self.__get_js_attributes(element)
86+
element.get_attribute = (
87+
lambda attribute: self.__get_attribute(element, attribute)
88+
)
8689
return element
8790

8891
def get(self, url):
@@ -440,6 +443,12 @@ def __get_js_attributes(self, element):
440443
self.loop.run_until_complete(element.get_js_attributes_async())
441444
)
442445

446+
def __get_attribute(self, element, attribute):
447+
try:
448+
return element.get_js_attributes()[attribute]
449+
except Exception:
450+
return None
451+
443452
def __get_x_scroll_offset(self):
444453
x_scroll_offset = self.loop.run_until_complete(
445454
self.page.evaluate("window.pageXOffset")
@@ -1013,8 +1022,10 @@ def get_element_attributes(self, selector):
10131022
)
10141023

10151024
def get_element_attribute(self, selector, attribute):
1016-
attributes = self.get_element_attributes(selector)
1017-
return attributes[attribute]
1025+
return self.get_element_attributes(selector)[attribute]
1026+
1027+
def get_attribute(self, selector, attribute):
1028+
return self.find_element(selector).get_attribute(attribute)
10181029

10191030
def get_element_html(self, selector):
10201031
selector = self.__convert_to_css_if_xpath(selector)
@@ -1774,31 +1785,26 @@ def scroll_to_y(self, y):
17741785
with suppress(Exception):
17751786
self.loop.run_until_complete(self.page.evaluate(js_code))
17761787
self.loop.run_until_complete(self.page.wait())
1777-
self.__add_light_pause()
17781788

17791789
def scroll_to_top(self):
17801790
js_code = "window.scrollTo(0, 0);"
17811791
with suppress(Exception):
17821792
self.loop.run_until_complete(self.page.evaluate(js_code))
17831793
self.loop.run_until_complete(self.page.wait())
1784-
self.__add_light_pause()
17851794

17861795
def scroll_to_bottom(self):
17871796
js_code = "window.scrollTo(0, 10000);"
17881797
with suppress(Exception):
17891798
self.loop.run_until_complete(self.page.evaluate(js_code))
17901799
self.loop.run_until_complete(self.page.wait())
1791-
self.__add_light_pause()
17921800

17931801
def scroll_up(self, amount=25):
17941802
self.loop.run_until_complete(self.page.scroll_up(amount))
17951803
self.loop.run_until_complete(self.page.wait())
1796-
self.__add_light_pause()
17971804

17981805
def scroll_down(self, amount=25):
17991806
self.loop.run_until_complete(self.page.scroll_down(amount))
18001807
self.loop.run_until_complete(self.page.wait())
1801-
self.__add_light_pause()
18021808

18031809
def save_screenshot(self, name, folder=None, selector=None):
18041810
filename = name

seleniumbase/core/settings_parser.py

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ def set_settings(settings_file):
7171
settings.LARGE_TIMEOUT = override_settings[key]
7272
elif key == "EXTREME_TIMEOUT":
7373
settings.EXTREME_TIMEOUT = override_settings[key]
74+
elif key == "PAGE_LOAD_TIMEOUT":
75+
settings.PAGE_LOAD_TIMEOUT = override_settings[key]
7476
elif key == "ARCHIVE_EXISTING_LOGS":
7577
settings.ARCHIVE_EXISTING_LOGS = override_settings[key]
7678
elif key == "ARCHIVE_EXISTING_DOWNLOADS":

seleniumbase/fixtures/base_case.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -6051,10 +6051,8 @@ def highlight(
60516051
scroll - the option to scroll to the element first (Default: True)
60526052
timeout - the time to wait for the element to appear """
60536053
self.__check_scope()
6054-
if self.__is_cdp_swap_needed() and ":contains(" not in selector:
6055-
self.cdp.highlight(selector)
6056-
return
6057-
self._check_browser()
6054+
if not self.__is_cdp_swap_needed():
6055+
self._check_browser()
60586056
self.__skip_if_esc()
60596057
if isinstance(selector, WebElement):
60606058
self.__highlight_element(selector, loops=loops, scroll=scroll)
@@ -13860,7 +13858,8 @@ def __slow_scroll_to_element(self, element):
1386013858
js_utils.scroll_to_element(self.driver, element)
1386113859

1386213860
def __highlight_with_js(self, selector, loops, o_bs):
13863-
self.wait_for_ready_state_complete()
13861+
if not self.__is_cdp_swap_needed():
13862+
self.wait_for_ready_state_complete()
1386413863
js_utils.highlight_with_js(self.driver, selector, loops, o_bs)
1386513864

1386613865
def __highlight_element_with_js(self, element, loops, o_bs):

seleniumbase/undetected/cdp_driver/config.py

-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ def __init__(
125125
"--deny-permission-prompts",
126126
"--disable-infobars",
127127
"--disable-breakpad",
128-
"--disable-component-update",
129128
"--disable-prompt-on-repost",
130129
"--disable-password-generation",
131130
"--disable-ipc-flooding-protection",

seleniumbase/undetected/cdp_driver/element.py

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22
import asyncio
3-
import json
43
import logging
54
import pathlib
65
import secrets
@@ -387,18 +386,16 @@ async def click_async(self):
387386

388387
async def get_js_attributes_async(self):
389388
return ContraDict(
390-
json.loads(
391-
await self.apply(
392-
"""
393-
function (e) {
394-
let o = {}
395-
for(let k in e){
396-
o[k] = e[k]
389+
await self.apply(
390+
"""
391+
function (e) {
392+
let o = {}
393+
for(let k in e){
394+
o[k] = e[k]
395+
}
396+
return o
397397
}
398-
return JSON.stringify(o)
399-
}
400-
"""
401-
)
398+
"""
402399
)
403400
)
404401

0 commit comments

Comments
 (0)