Skip to content

Commit 8decf8d

Browse files
committed
Update CDP Mode
1 parent 30caa81 commit 8decf8d

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

seleniumbase/core/browser_launcher.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,7 @@ def uc_open_with_cdp_mode(driver, url=None):
612612
cdp.save_cookies = CDPM.save_cookies
613613
cdp.load_cookies = CDPM.load_cookies
614614
cdp.clear_cookies = CDPM.clear_cookies
615+
cdp.sleep = CDPM.sleep
615616
cdp.bring_active_window_to_front = CDPM.bring_active_window_to_front
616617
cdp.bring_to_front = CDPM.bring_active_window_to_front
617618
cdp.get_active_element = CDPM.get_active_element
@@ -684,6 +685,7 @@ def uc_open_with_cdp_mode(driver, url=None):
684685
cdp.select_if_unselected = CDPM.select_if_unselected
685686
cdp.unselect_if_selected = CDPM.unselect_if_selected
686687
cdp.is_checked = CDPM.is_checked
688+
cdp.is_selected = CDPM.is_selected
687689
cdp.is_element_present = CDPM.is_element_present
688690
cdp.is_element_visible = CDPM.is_element_visible
689691
cdp.wait_for_element_visible = CDPM.wait_for_element_visible
@@ -699,6 +701,8 @@ def uc_open_with_cdp_mode(driver, url=None):
699701
cdp.assert_url_contains = CDPM.assert_url_contains
700702
cdp.assert_text = CDPM.assert_text
701703
cdp.assert_exact_text = CDPM.assert_exact_text
704+
cdp.assert_true = CDPM.assert_true
705+
cdp.assert_false = CDPM.assert_false
702706
cdp.scroll_into_view = CDPM.scroll_into_view
703707
cdp.scroll_to_y = CDPM.scroll_to_y
704708
cdp.scroll_to_top = CDPM.scroll_to_top
@@ -1167,7 +1171,12 @@ def _uc_gui_click_captcha(
11671171
frame = "%s div" % frame
11681172
elif (
11691173
driver.is_element_present('[name*="cf-turnstile-"]')
1170-
and driver.is_element_present('[class*=spacer] + div div')
1174+
and driver.is_element_present("#challenge-form div > div")
1175+
):
1176+
frame = "#challenge-form div > div"
1177+
elif (
1178+
driver.is_element_present('[name*="cf-turnstile-"]')
1179+
and driver.is_element_present("[class*=spacer] + div div")
11711180
):
11721181
frame = '[class*=spacer] + div div'
11731182
elif (
@@ -1240,8 +1249,8 @@ def _uc_gui_click_captcha(
12401249
return
12411250
try:
12421251
if ctype == "g_rc" and not driver.is_connected():
1243-
x = (i_x + 32) * width_ratio
1244-
y = (i_y + 34) * width_ratio
1252+
x = (i_x + 29) * width_ratio
1253+
y = (i_y + 35) * width_ratio
12451254
elif visible_iframe:
12461255
selector = "span"
12471256
if ctype == "g_rc":
@@ -1256,8 +1265,8 @@ def _uc_gui_click_captcha(
12561265
y = i_y + element.rect["y"] + (element.rect["height"] / 2.0)
12571266
y += 0.5
12581267
else:
1259-
x = (i_x + 34) * width_ratio
1260-
y = (i_y + 34) * width_ratio
1268+
x = (i_x + 32) * width_ratio
1269+
y = (i_y + 32) * width_ratio
12611270
if driver.is_connected():
12621271
driver.switch_to.default_content()
12631272
except Exception:
@@ -1497,6 +1506,7 @@ def _uc_gui_handle_captcha_(driver, frame="iframe", ctype=None):
14971506
tab_count += 1
14981507
time.sleep(0.027)
14991508
active_element_css = js_utils.get_active_element_css(driver)
1509+
print(active_element_css)
15001510
if (
15011511
active_element_css.startswith(selector)
15021512
or active_element_css.endswith(" > div" * 2)
@@ -1514,7 +1524,10 @@ def _uc_gui_handle_captcha_(driver, frame="iframe", ctype=None):
15141524
except Exception:
15151525
return
15161526
if (
1517-
driver.is_element_present(".footer .clearfix .ray-id")
1527+
(
1528+
driver.is_element_present(".footer .clearfix .ray-id")
1529+
or driver.is_element_present("script[data-cf-beacon]")
1530+
)
15181531
and hasattr(sb_config, "_saved_cf_tab_count")
15191532
and sb_config._saved_cf_tab_count
15201533
):

seleniumbase/core/sb_cdp.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,6 +1681,14 @@ def assert_exact_text(
16811681
% (text, element.text_all, selector)
16821682
)
16831683

1684+
def assert_true(self, expression):
1685+
if not expression:
1686+
raise AssertionError("%s is not true")
1687+
1688+
def assert_false(self, expression):
1689+
if expression:
1690+
raise AssertionError("%s is not false")
1691+
16841692
def scroll_into_view(self, selector):
16851693
self.find_element(selector).scroll_into_view()
16861694
self.loop.run_until_complete(self.page.wait())

seleniumbase/undetected/cdp_driver/browser.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
import json
77
import logging
88
import os
9-
import pickle
109
import pathlib
10+
import pickle
11+
import re
1112
import shutil
1213
import urllib.parse
1314
import urllib.request
@@ -644,7 +645,7 @@ async def get_all(
644645
"""
645646
connection = None
646647
for _tab in self._browser.tabs:
647-
if _tab.closed:
648+
if hasattr(_tab, "closed") and _tab.closed:
648649
continue
649650
connection = _tab
650651
break
@@ -674,7 +675,7 @@ async def set_all(self, cookies: List[cdp.network.CookieParam]):
674675
"""
675676
connection = None
676677
for _tab in self._browser.tabs:
677-
if _tab.closed:
678+
if hasattr(_tab, "closed") and _tab.closed:
678679
continue
679680
connection = _tab
680681
break
@@ -698,13 +699,11 @@ async def save(self, file: PathLike = ".session.dat", pattern: str = ".*"):
698699
- Contain "nowsecure"
699700
:type pattern: str
700701
"""
701-
import re
702-
703702
pattern = re.compile(pattern)
704703
save_path = pathlib.Path(file).resolve()
705704
connection = None
706705
for _tab in self._browser.tabs:
707-
if _tab.closed:
706+
if hasattr(_tab, "closed") and _tab.closed:
708707
continue
709708
connection = _tab
710709
break
@@ -746,15 +745,13 @@ async def load(self, file: PathLike = ".session.dat", pattern: str = ".*"):
746745
- Contain "nowsecure"
747746
:type pattern: str
748747
"""
749-
import re
750-
751748
pattern = re.compile(pattern)
752749
save_path = pathlib.Path(file).resolve()
753750
cookies = pickle.load(save_path.open("r+b"))
754751
included_cookies = []
755752
connection = None
756753
for _tab in self._browser.tabs:
757-
if _tab.closed:
754+
if hasattr(_tab, "closed") and _tab.closed:
758755
continue
759756
connection = _tab
760757
break
@@ -779,7 +776,7 @@ async def clear(self):
779776
"""
780777
connection = None
781778
for _tab in self._browser.tabs:
782-
if _tab.closed:
779+
if hasattr(_tab, "closed") and _tab.closed:
783780
continue
784781
connection = _tab
785782
break

0 commit comments

Comments
 (0)