Skip to content

Commit 02b34f7

Browse files
committed
Add more methods to CDP Mode
1 parent 1a691d8 commit 02b34f7

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

examples/cdp_mode/ReadMe.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,10 +460,14 @@ sb.cdp.get_element_attributes(selector)
460460
sb.cdp.get_element_attribute(selector, attribute)
461461
sb.cdp.get_attribute(selector, attribute)
462462
sb.cdp.get_element_html(selector)
463+
sb.cdp.get_mfa_code(totp_key=None)
464+
sb.cdp.enter_mfa_code(selector, totp_key=None, timeout=None)
463465
sb.cdp.set_locale(locale)
464466
sb.cdp.set_local_storage_item(key, value)
465467
sb.cdp.set_session_storage_item(key, value)
466468
sb.cdp.set_attributes(selector, attribute, value)
469+
sb.cdp.is_attribute_present(selector, attribute, value=None)
470+
sb.cdp.is_online()
467471
sb.cdp.gui_press_key(key)
468472
sb.cdp.gui_press_keys(keys)
469473
sb.cdp.gui_write(text)

seleniumbase/core/browser_launcher.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,8 @@ def uc_open_with_cdp_mode(driver, url=None, **kwargs):
766766
cdp.set_local_storage_item = CDPM.set_local_storage_item
767767
cdp.set_session_storage_item = CDPM.set_session_storage_item
768768
cdp.set_attributes = CDPM.set_attributes
769+
cdp.is_attribute_present = CDPM.is_attribute_present
770+
cdp.is_online = CDPM.is_online
769771
cdp.gui_press_key = CDPM.gui_press_key
770772
cdp.gui_press_keys = CDPM.gui_press_keys
771773
cdp.gui_write = CDPM.gui_write
@@ -815,10 +817,12 @@ def uc_open_with_cdp_mode(driver, url=None, **kwargs):
815817
cdp.get_screen_rect = CDPM.get_screen_rect
816818
cdp.get_window_rect = CDPM.get_window_rect
817819
cdp.get_window_size = CDPM.get_window_size
820+
cdp.get_mfa_code = CDPM.get_mfa_code
818821
cdp.nested_click = CDPM.nested_click
819822
cdp.select_option_by_text = CDPM.select_option_by_text
820823
cdp.select_option_by_index = CDPM.select_option_by_index
821824
cdp.select_option_by_value = CDPM.select_option_by_value
825+
cdp.enter_mfa_code = CDPM.enter_mfa_code
822826
cdp.flash = CDPM.flash
823827
cdp.highlight = CDPM.highlight
824828
cdp.focus = CDPM.focus

seleniumbase/core/sb_cdp.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,6 +1393,17 @@ def get_element_html(self, selector):
13931393
)
13941394
)
13951395

1396+
def get_mfa_code(self, totp_key=None):
1397+
"""Returns a time-based one-time password based on the Google
1398+
Authenticator algorithm for multi-factor authentication."""
1399+
return shared_utils.get_mfa_code(totp_key)
1400+
1401+
def enter_mfa_code(self, selector, totp_key=None, timeout=None):
1402+
if not timeout:
1403+
timeout = settings.SMALL_TIMEOUT
1404+
mfa_code = self.get_mfa_code(totp_key)
1405+
self.type(selector, mfa_code + "\n", timeout=timeout)
1406+
13961407
def set_locale(self, locale):
13971408
"""(Settings will take effect on the next page load)"""
13981409
self.loop.run_until_complete(self.page.set_locale(locale))
@@ -1430,6 +1441,26 @@ def set_attributes(self, selector, attribute, value):
14301441
with suppress(Exception):
14311442
self.loop.run_until_complete(self.page.evaluate(js_code))
14321443

1444+
def is_attribute_present(self, selector, attribute, value=None):
1445+
try:
1446+
element = self.find_element(selector, timeout=0.1)
1447+
found_value = element.get_attribute(attribute)
1448+
if found_value is None:
1449+
return False
1450+
if value is not None:
1451+
if found_value == value:
1452+
return True
1453+
else:
1454+
return False
1455+
else:
1456+
return True
1457+
except Exception:
1458+
return False
1459+
1460+
def is_online(self):
1461+
js_code = "navigator.onLine;"
1462+
return self.loop.run_until_complete(self.page.evaluate(js_code))
1463+
14331464
def __make_sure_pyautogui_lock_is_writable(self):
14341465
with suppress(Exception):
14351466
shared_utils.make_writable(constants.MultiBrowser.PYAUTOGUILOCK)

0 commit comments

Comments
 (0)