@@ -544,6 +544,8 @@ def uc_open_with_cdp_mode(driver, url=None):
544544 )
545545 loop .run_until_complete (driver .cdp_base .wait (0 ))
546546
547+ gui_lock = fasteners .InterProcessLock (constants .MultiBrowser .PYAUTOGUILOCK )
548+
547549 if (
548550 "chrome-extension://" in str (driver .cdp_base .main_tab )
549551 and len (driver .cdp_base .tabs ) >= 2
@@ -553,7 +555,8 @@ def uc_open_with_cdp_mode(driver, url=None):
553555
554556 for tab in driver .cdp_base .tabs [- 1 ::- 1 ]:
555557 if "chrome-extension://" not in str (tab ):
556- loop .run_until_complete (tab .activate ())
558+ with gui_lock :
559+ loop .run_until_complete (tab .activate ())
557560 break
558561
559562 page_tab = None
@@ -566,13 +569,20 @@ def uc_open_with_cdp_mode(driver, url=None):
566569 break
567570 if page_tab :
568571 loop .run_until_complete (page_tab .aopen ())
569- loop .run_until_complete (page_tab .activate ())
572+ with gui_lock :
573+ loop .run_until_complete (page_tab .activate ())
570574
571575 loop .run_until_complete (driver .cdp_base .update_targets ())
572576 page = loop .run_until_complete (driver .cdp_base .get (url ))
573- loop .run_until_complete (page .activate ())
577+ with gui_lock :
578+ loop .run_until_complete (page .activate ())
579+ loop .run_until_complete (page .wait ())
574580 if not safe_url :
575581 time .sleep (constants .UC .CDP_MODE_OPEN_WAIT )
582+ if IS_WINDOWS :
583+ time .sleep (constants .UC .EXTRA_WINDOWS_WAIT )
584+ else :
585+ time .sleep (0.012 )
576586 cdp = types .SimpleNamespace ()
577587 CDPM = sb_cdp .CDPMethods (loop , page , driver )
578588 cdp .get = CDPM .get
@@ -603,14 +613,15 @@ def uc_open_with_cdp_mode(driver, url=None):
603613 cdp .click = CDPM .click
604614 cdp .click_active_element = CDPM .click_active_element
605615 cdp .click_if_visible = CDPM .click_if_visible
616+ cdp .click_visible_elements = CDPM .click_visible_elements
606617 cdp .mouse_click = CDPM .mouse_click
607618 cdp .remove_element = CDPM .remove_element
608619 cdp .remove_from_dom = CDPM .remove_from_dom
609620 cdp .remove_elements = CDPM .remove_elements
610- cdp .scroll_into_view = CDPM .scroll_into_view
611621 cdp .send_keys = CDPM .send_keys
612622 cdp .press_keys = CDPM .press_keys
613623 cdp .type = CDPM .type
624+ cdp .set_value = CDPM .set_value
614625 cdp .evaluate = CDPM .evaluate
615626 cdp .js_dumps = CDPM .js_dumps
616627 cdp .maximize = CDPM .maximize
@@ -625,6 +636,11 @@ def uc_open_with_cdp_mode(driver, url=None):
625636 cdp .gui_write = CDPM .gui_write
626637 cdp .gui_click_x_y = CDPM .gui_click_x_y
627638 cdp .gui_click_element = CDPM .gui_click_element
639+ cdp .gui_drag_drop_points = CDPM .gui_drag_drop_points
640+ cdp .gui_drag_and_drop = CDPM .gui_drag_and_drop
641+ cdp .gui_hover_x_y = CDPM .gui_hover_x_y
642+ cdp .gui_hover_element = CDPM .gui_hover_element
643+ cdp .gui_hover_and_click = CDPM .gui_hover_and_click
628644 cdp .internalize_links = CDPM .internalize_links
629645 cdp .get_window = CDPM .get_window
630646 cdp .get_element_attributes = CDPM .get_element_attributes
@@ -651,7 +667,9 @@ def uc_open_with_cdp_mode(driver, url=None):
651667 cdp .get_window_rect = CDPM .get_window_rect
652668 cdp .get_window_size = CDPM .get_window_size
653669 cdp .nested_click = CDPM .nested_click
670+ cdp .select_option_by_text = CDPM .select_option_by_text
654671 cdp .flash = CDPM .flash
672+ cdp .highlight = CDPM .highlight
655673 cdp .focus = CDPM .focus
656674 cdp .highlight_overlay = CDPM .highlight_overlay
657675 cdp .get_window_position = CDPM .get_window_position
@@ -663,12 +681,19 @@ def uc_open_with_cdp_mode(driver, url=None):
663681 cdp .is_element_present = CDPM .is_element_present
664682 cdp .is_element_visible = CDPM .is_element_visible
665683 cdp .assert_element_present = CDPM .assert_element_present
684+ cdp .assert_element_absent = CDPM .assert_element_absent
666685 cdp .assert_element = CDPM .assert_element
667686 cdp .assert_element_visible = CDPM .assert_element
687+ cdp .assert_element_not_visible = CDPM .assert_element_not_visible
688+ cdp .assert_title = CDPM .assert_title
668689 cdp .assert_text = CDPM .assert_text
669690 cdp .assert_exact_text = CDPM .assert_exact_text
670- cdp .scroll_down = CDPM .scroll_down
691+ cdp .scroll_into_view = CDPM .scroll_into_view
692+ cdp .scroll_to_y = CDPM .scroll_to_y
693+ cdp .scroll_to_top = CDPM .scroll_to_top
694+ cdp .scroll_to_bottom = CDPM .scroll_to_bottom
671695 cdp .scroll_up = CDPM .scroll_up
696+ cdp .scroll_down = CDPM .scroll_down
672697 cdp .save_screenshot = CDPM .save_screenshot
673698 cdp .page = page # async world
674699 cdp .driver = driver .cdp_base # async world
@@ -680,6 +705,7 @@ def uc_open_with_cdp_mode(driver, url=None):
680705 core_items .tab = cdp .tab
681706 core_items .util = cdp .util
682707 cdp .core = core_items
708+ cdp .loop = cdp .get_event_loop ()
683709 driver .cdp = cdp
684710 driver ._is_using_cdp = True
685711
@@ -944,48 +970,46 @@ def uc_gui_click_x_y(driver, x, y, timeframe=0.25):
944970 connected = True
945971 width_ratio = 1.0
946972 if IS_WINDOWS :
947- try :
948- driver .window_handles
949- except Exception :
950- connected = False
973+ connected = driver .is_connected ()
951974 if (
952975 not connected
953976 and (
954977 not hasattr (sb_config , "_saved_width_ratio" )
955978 or not sb_config ._saved_width_ratio
956979 )
980+ and not __is_cdp_swap_needed (driver )
957981 ):
958982 driver .reconnect (0.1 )
959- connected = True
960- if IS_WINDOWS and connected :
983+ if IS_WINDOWS and not __is_cdp_swap_needed (driver ):
961984 window_rect = driver .get_window_rect ()
962985 width = window_rect ["width" ]
963986 height = window_rect ["height" ]
964987 win_x = window_rect ["x" ]
965988 win_y = window_rect ["y" ]
966- if (
967- hasattr (sb_config , "_saved_width_ratio" )
968- and sb_config ._saved_width_ratio
969- ):
970- width_ratio = sb_config ._saved_width_ratio
971- else :
972- scr_width = pyautogui .size ().width
973- driver .maximize_window ()
974- win_width = driver .get_window_size ()["width" ]
975- width_ratio = round (float (scr_width ) / float (win_width ), 2 )
976- width_ratio += 0.01
977- if width_ratio < 0.45 or width_ratio > 2.55 :
978- width_ratio = 1.01
979- sb_config ._saved_width_ratio = width_ratio
989+ scr_width = pyautogui .size ().width
990+ driver .maximize_window ()
991+ win_width = driver .get_window_size ()["width" ]
992+ width_ratio = round (float (scr_width ) / float (win_width ), 2 ) + 0.01
993+ if width_ratio < 0.45 or width_ratio > 2.55 :
994+ width_ratio = 1.01
995+ sb_config ._saved_width_ratio = width_ratio
980996 driver .minimize_window ()
981997 driver .set_window_rect (win_x , win_y , width , height )
982- elif (
983- IS_WINDOWS
984- and not connected
985- and hasattr (sb_config , "_saved_width_ratio" )
986- and sb_config ._saved_width_ratio
987- ):
988- width_ratio = sb_config ._saved_width_ratio
998+ elif IS_WINDOWS and __is_cdp_swap_needed (driver ):
999+ window_rect = driver .cdp .get_window_rect ()
1000+ width = window_rect ["width" ]
1001+ height = window_rect ["height" ]
1002+ win_x = window_rect ["x" ]
1003+ win_y = window_rect ["y" ]
1004+ scr_width = pyautogui .size ().width
1005+ driver .cdp .maximize ()
1006+ win_width = driver .cdp .get_window_size ()["width" ]
1007+ width_ratio = round (float (scr_width ) / float (win_width ), 2 ) + 0.01
1008+ if width_ratio < 0.45 or width_ratio > 2.55 :
1009+ width_ratio = 1.01
1010+ sb_config ._saved_width_ratio = width_ratio
1011+ driver .cdp .minimize ()
1012+ driver .cdp .set_window_rect (win_x , win_y , width , height )
9891013 if IS_WINDOWS :
9901014 x = x * width_ratio
9911015 y = y * width_ratio
@@ -1090,6 +1114,21 @@ def _uc_gui_click_captcha(
10901114 sb_config ._saved_width_ratio = width_ratio
10911115 driver .minimize_window ()
10921116 driver .set_window_rect (win_x , win_y , width , height )
1117+ elif IS_WINDOWS and __is_cdp_swap_needed (driver ):
1118+ window_rect = driver .cdp .get_window_rect ()
1119+ width = window_rect ["width" ]
1120+ height = window_rect ["height" ]
1121+ win_x = window_rect ["x" ]
1122+ win_y = window_rect ["y" ]
1123+ scr_width = pyautogui .size ().width
1124+ driver .cdp .maximize ()
1125+ win_width = driver .cdp .get_window_size ()["width" ]
1126+ width_ratio = round (float (scr_width ) / float (win_width ), 2 ) + 0.01
1127+ if width_ratio < 0.45 or width_ratio > 2.55 :
1128+ width_ratio = 1.01
1129+ sb_config ._saved_width_ratio = width_ratio
1130+ driver .cdp .minimize ()
1131+ driver .cdp .set_window_rect (win_x , win_y , width , height )
10931132 if ctype == "cf_t" :
10941133 if (
10951134 driver .is_element_present (".cf-turnstile-wrapper iframe" )
@@ -1201,10 +1240,10 @@ def _uc_gui_click_captcha(
12011240 element = driver .wait_for_element_present (
12021241 selector , timeout = 2.5
12031242 )
1204- x = i_x + element .rect ["x" ] + int (element .rect ["width" ] / 2 )
1205- x += 1
1206- y = i_y + element .rect ["y" ] + int (element .rect ["height" ] / 2 )
1207- y += 1
1243+ x = i_x + element .rect ["x" ] + (element .rect ["width" ] / 2.0 )
1244+ x += 0.5
1245+ y = i_y + element .rect ["y" ] + (element .rect ["height" ] / 2.0 )
1246+ y += 0.5
12081247 else :
12091248 x = (i_x + 34 ) * width_ratio
12101249 y = (i_y + 34 ) * width_ratio
@@ -1218,12 +1257,18 @@ def _uc_gui_click_captcha(
12181257 return
12191258 if x and y :
12201259 sb_config ._saved_cf_x_y = (x , y )
1221- if driver .is_element_present (".footer .clearfix .ray-id" ):
1222- driver .uc_open_with_disconnect (driver .get_current_url (), 3.8 )
1223- else :
1224- driver .disconnect ()
1260+ if not __is_cdp_swap_needed (driver ):
1261+ if driver .is_element_present (".footer .clearfix .ray-id" ):
1262+ driver .uc_open_with_disconnect (
1263+ driver .get_current_url (), 3.8
1264+ )
1265+ else :
1266+ driver .disconnect ()
12251267 with suppress (Exception ):
12261268 _uc_gui_click_x_y (driver , x , y , timeframe = 0.32 )
1269+ if __is_cdp_swap_needed (driver ):
1270+ time .sleep (float (constants .UC .RECONNECT_TIME ) / 2.0 )
1271+ return
12271272 reconnect_time = (float (constants .UC .RECONNECT_TIME ) / 2.0 ) + 0.6
12281273 if IS_LINUX :
12291274 reconnect_time = constants .UC .RECONNECT_TIME + 0.2
0 commit comments