Skip to content

Commit 9033296

Browse files
committedDec 15, 2024
Update CDP Mode
1 parent c2d144e commit 9033296

File tree

3 files changed

+39
-16
lines changed

3 files changed

+39
-16
lines changed
 

‎seleniumbase/core/sb_cdp.py

+28-7
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,20 @@ def __get_attribute(self, element, attribute):
519519
try:
520520
return element.get_js_attributes()[attribute]
521521
except Exception:
522-
return None
522+
if not attribute:
523+
raise
524+
try:
525+
attribute_str = element.get_js_attributes()
526+
locate = ' %s="' % attribute
527+
if locate in attribute_str.outerHTML:
528+
outer_html = attribute_str.outerHTML
529+
attr_start = outer_html.find(locate) + len(locate)
530+
attr_end = outer_html.find('"', attr_start)
531+
value = outer_html[attr_start:attr_end]
532+
return value
533+
except Exception:
534+
pass
535+
return None
523536

524537
def __get_x_scroll_offset(self):
525538
x_scroll_offset = self.loop.run_until_complete(
@@ -620,11 +633,12 @@ def click_active_element(self):
620633

621634
def click_if_visible(self, selector):
622635
if self.is_element_visible(selector):
623-
element = self.find_element(selector)
624-
element.scroll_into_view()
625-
element.click()
626-
self.__slow_mode_pause_if_set()
627-
self.loop.run_until_complete(self.page.wait())
636+
with suppress(Exception):
637+
element = self.find_element(selector, timeout=0)
638+
element.scroll_into_view()
639+
element.click()
640+
self.__slow_mode_pause_if_set()
641+
self.loop.run_until_complete(self.page.wait())
628642

629643
def click_visible_elements(self, selector, limit=0):
630644
"""Finds all matching page elements and clicks visible ones in order.
@@ -1094,7 +1108,14 @@ def get_element_attributes(self, selector):
10941108
)
10951109

10961110
def get_element_attribute(self, selector, attribute):
1097-
return self.get_element_attributes(selector)[attribute]
1111+
attributes = self.get_element_attributes(selector)
1112+
with suppress(Exception):
1113+
return attributes[attribute]
1114+
locate = ' %s="' % attribute
1115+
value = self.get_attribute(selector, attribute)
1116+
if not value and locate not in attributes:
1117+
raise KeyError(attribute)
1118+
return value
10981119

10991120
def get_attribute(self, selector, attribute):
11001121
return self.find_element(selector).get_attribute(attribute)

‎seleniumbase/undetected/cdp_driver/_contradict.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ class ContraDict(dict):
3131

3232
def __init__(self, *args, **kwargs):
3333
super().__init__()
34-
silent = kwargs.pop("silent", False)
34+
# silent = kwargs.pop("silent", False)
3535
_ = dict(*args, **kwargs)
3636

3737
super().__setattr__("__dict__", self)
3838
for k, v in _.items():
39-
_check_key(k, self, False, silent)
39+
_check_key(k, self, False, True)
4040
super().__setitem__(k, _wrap(self.__class__, v))
4141

4242
def __setitem__(self, key, value):
@@ -90,7 +90,7 @@ def _wrap(cls, v):
9090

9191

9292
def _check_key(
93-
key: str, mapping: _Mapping, boolean: bool = False, silent=False
93+
key: str, mapping: _Mapping, boolean: bool = False, silent=True
9494
):
9595
"""Checks `key` and warns if needed.
9696
:param key:

‎seleniumbase/undetected/cdp_driver/browser.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import pickle
1111
import re
1212
import shutil
13+
import time
1314
import urllib.parse
1415
import urllib.request
1516
import warnings
@@ -30,8 +31,6 @@ def get_registered_instances():
3031

3132

3233
def deconstruct_browser():
33-
import time
34-
3534
for _ in __registered__instances__:
3635
if not _.stopped:
3736
_.stop()
@@ -117,8 +116,13 @@ async def create(
117116
port=port,
118117
**kwargs,
119118
)
120-
instance = cls(config)
121-
await instance.start()
119+
try:
120+
instance = cls(config)
121+
await instance.start()
122+
except Exception:
123+
time.sleep(0.15)
124+
instance = cls(config)
125+
await instance.start()
122126
return instance
123127

124128
def __init__(self, config: Config, **kwargs):
@@ -379,8 +383,6 @@ async def start(self=None) -> Browser:
379383
--------------------------------
380384
Failed to connect to the browser
381385
--------------------------------
382-
Possibly because you are running as "root".
383-
If so, you may need to use no_sandbox=True.
384386
"""
385387
)
386388
)

0 commit comments

Comments
 (0)
Please sign in to comment.