Skip to content

Commit 660ee46

Browse files
committed
Update CDP Mode
1 parent a0ae330 commit 660ee46

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

examples/cdp_mode/ReadMe.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ sb.cdp.gui_press_keys(keys)
465465
sb.cdp.gui_write(text)
466466
sb.cdp.gui_click_x_y(x, y, timeframe=0.25)
467467
sb.cdp.gui_click_element(selector, timeframe=0.25)
468+
sb.cdp.gui_click_captcha()
468469
sb.cdp.gui_drag_drop_points(x1, y1, x2, y2, timeframe=0.35)
469470
sb.cdp.gui_drag_and_drop(drag_selector, drop_selector, timeframe=0.35)
470471
sb.cdp.gui_click_and_hold(selector, timeframe=0.35)

seleniumbase/core/browser_launcher.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,7 @@ def uc_open_with_cdp_mode(driver, url=None, **kwargs):
762762
cdp.gui_write = CDPM.gui_write
763763
cdp.gui_click_x_y = CDPM.gui_click_x_y
764764
cdp.gui_click_element = CDPM.gui_click_element
765+
cdp.gui_click_captcha = CDPM.gui_click_captcha
765766
cdp.gui_drag_drop_points = CDPM.gui_drag_drop_points
766767
cdp.gui_drag_and_drop = CDPM.gui_drag_and_drop
767768
cdp.gui_click_and_hold = CDPM.gui_click_and_hold

seleniumbase/core/sb_cdp.py

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,6 +1629,152 @@ def gui_click_element(self, selector, timeframe=0.25):
16291629
self.__slow_mode_pause_if_set()
16301630
self.loop.run_until_complete(self.page.wait())
16311631

1632+
def _on_a_cf_turnstile_page(self):
1633+
source = self.get_page_source()
1634+
if (
1635+
'data-callback="onCaptchaSuccess"' in source
1636+
or "/challenge-platform/scripts/" in source
1637+
or 'id="challenge-widget-' in source
1638+
or "cf-turnstile-" in source
1639+
):
1640+
return True
1641+
return False
1642+
1643+
def gui_click_captcha(self):
1644+
if not self._on_a_cf_turnstile_page():
1645+
return
1646+
selector = None
1647+
if (
1648+
self.is_element_present('[name*="cf-turnstile-"]')
1649+
and self.is_element_present("#challenge-form div > div")
1650+
):
1651+
selector = "#challenge-form div > div"
1652+
elif (
1653+
self.is_element_present('[name*="cf-turnstile-"]')
1654+
and self.is_element_present(
1655+
'[style="display: grid;"] div div'
1656+
)
1657+
):
1658+
selector = '[style="display: grid;"] div div'
1659+
elif (
1660+
self.is_element_present('[name*="cf-turnstile-"]')
1661+
and self.is_element_present("[class*=spacer] + div div")
1662+
):
1663+
selector = '[class*=spacer] + div div'
1664+
elif (
1665+
self.is_element_present('[name*="cf-turnstile-"]')
1666+
and self.is_element_present("div.spacer div")
1667+
):
1668+
selector = "div.spacer div"
1669+
elif (
1670+
self.is_element_present('script[src*="challenges.c"]')
1671+
and self.is_element_present(
1672+
'[data-testid*="challenge-"] div'
1673+
)
1674+
):
1675+
selector = '[data-testid*="challenge-"] div'
1676+
elif self.is_element_present(
1677+
"div#turnstile-widget div:not([class])"
1678+
):
1679+
selector = "div#turnstile-widget div:not([class])"
1680+
elif self.is_element_present(
1681+
'form div:not([class]):has(input[name*="cf-turn"])'
1682+
):
1683+
selector = 'form div:not([class]):has(input[name*="cf-turn"])'
1684+
elif (
1685+
self.is_element_present('[src*="/turnstile/"]')
1686+
and self.is_element_present("form div:not(:has(*))")
1687+
):
1688+
selector = "form div:not(:has(*))"
1689+
elif (
1690+
self.is_element_present('[src*="/turnstile/"]')
1691+
and self.is_element_present(
1692+
"body > div#check > div:not([class])"
1693+
)
1694+
):
1695+
selector = "body > div#check > div:not([class])"
1696+
elif self.is_element_present(".cf-turnstile-wrapper"):
1697+
selector = ".cf-turnstile-wrapper"
1698+
elif self.is_element_present('[class="cf-turnstile"]'):
1699+
selector = '[class="cf-turnstile"]'
1700+
elif self.is_element_present(
1701+
'[data-callback="onCaptchaSuccess"]'
1702+
):
1703+
selector = '[data-callback="onCaptchaSuccess"]'
1704+
else:
1705+
return
1706+
if not selector:
1707+
return
1708+
if (
1709+
self.is_element_present("form")
1710+
and (
1711+
self.is_element_present('form[class*="center"]')
1712+
or self.is_element_present('form[class*="right"]')
1713+
or self.is_element_present('form div[class*="center"]')
1714+
or self.is_element_present('form div[class*="right"]')
1715+
)
1716+
):
1717+
script = (
1718+
"""var $elements = document.querySelectorAll(
1719+
'form[class], form div[class]');
1720+
var index = 0, length = $elements.length;
1721+
for(; index < length; index++){
1722+
the_class = $elements[index].getAttribute('class');
1723+
new_class = the_class.replaceAll('center', 'left');
1724+
new_class = new_class.replaceAll('right', 'left');
1725+
$elements[index].setAttribute('class', new_class);}"""
1726+
)
1727+
with suppress(Exception):
1728+
self.loop.run_until_complete(self.page.evaluate(script))
1729+
self.loop.run_until_complete(self.page.wait())
1730+
elif (
1731+
self.is_element_present("form")
1732+
and (
1733+
self.is_element_present('form div[style*="center"]')
1734+
or self.is_element_present('form div[style*="right"]')
1735+
)
1736+
):
1737+
script = (
1738+
"""var $elements = document.querySelectorAll(
1739+
'form[style], form div[style]');
1740+
var index = 0, length = $elements.length;
1741+
for(; index < length; index++){
1742+
the_style = $elements[index].getAttribute('style');
1743+
new_style = the_style.replaceAll('center', 'left');
1744+
new_style = new_style.replaceAll('right', 'left');
1745+
$elements[index].setAttribute('style', new_style);}"""
1746+
)
1747+
with suppress(Exception):
1748+
self.loop.run_until_complete(self.page.evaluate(script))
1749+
self.loop.run_until_complete(self.page.wait())
1750+
elif (
1751+
self.is_element_present("form")
1752+
and self.is_element_present(
1753+
'form [id*="turnstile"] > div:not([class])'
1754+
)
1755+
):
1756+
script = (
1757+
"""var $elements = document.querySelectorAll(
1758+
'form [id*="turnstile"]');
1759+
var index = 0, length = $elements.length;
1760+
for(; index < length; index++){
1761+
$elements[index].setAttribute('align', 'left');}"""
1762+
)
1763+
with suppress(Exception):
1764+
self.loop.run_until_complete(self.page.evaluate(script))
1765+
self.loop.run_until_complete(self.page.wait())
1766+
with suppress(Exception):
1767+
element_rect = self.get_gui_element_rect(selector, timeout=1)
1768+
e_x = element_rect["x"]
1769+
e_y = element_rect["y"]
1770+
x = e_x + 32
1771+
if not shared_utils.is_windows():
1772+
y = e_y + 32
1773+
else:
1774+
y = e_y + 22
1775+
sb_config._saved_cf_x_y = (x, y)
1776+
self.gui_click_x_y(x, y)
1777+
16321778
def __gui_drag_drop(self, x1, y1, x2, y2, timeframe=0.25, uc_lock=False):
16331779
self.__install_pyautogui_if_missing()
16341780
import pyautogui

0 commit comments

Comments
 (0)