@@ -66,6 +66,7 @@ def __add_sync_methods(self, element):
6666 lambda * args , ** kwargs : self .__gui_click (element , * args , ** kwargs )
6767 )
6868 element .highlight_overlay = lambda : self .__highlight_overlay (element )
69+ element .is_in_viewport = lambda : self .__is_in_viewport (element )
6970 element .mouse_click = lambda : self .__mouse_click (element )
7071 element .click_with_offset = (
7172 lambda * args , ** kwargs : self .__mouse_click_with_offset_async (
@@ -543,6 +544,11 @@ def __highlight_overlay(self, element):
543544 self .loop .run_until_complete (element .highlight_overlay_async ())
544545 )
545546
547+ def __is_in_viewport (self , element ):
548+ return (
549+ self .loop .run_until_complete (element .is_in_viewport_async ())
550+ )
551+
546552 def __mouse_click (self , element ):
547553 result = (
548554 self .loop .run_until_complete (element .mouse_click_async ())
@@ -800,22 +806,34 @@ def click(self, selector, timeout=None, scroll=True):
800806 timeout = settings .SMALL_TIMEOUT
801807 self .__slow_mode_pause_if_set ()
802808 element = self .find_element (selector , timeout = timeout )
803- if scroll :
804- element .scroll_into_view ()
805809 tag_name = element .tag_name
810+ current_url = self .get_current_url ()
811+
806812 if tag_name :
807813 tag_name = tag_name .lower ().strip ()
808814 if (
809815 tag_name in [
810816 "a" , "button" , "canvas" , "div" , "input" , "li" , "span" , "label"
811817 ]
812818 and "contains(" not in selector
819+ and "://google" not in current_url
820+ and "://www.google" not in current_url
813821 ):
822+ if scroll :
823+ element .scroll_into_view ()
814824 try :
815825 element .mouse_click () # Simulated click (NOT PyAutoGUI)
816826 except Exception :
817827 element .click () # Standard CDP click
818828 else :
829+ if scroll :
830+ if "contains(" in selector :
831+ element .scroll_into_view ()
832+ else :
833+ try :
834+ self .js_scroll_into_view (selector )
835+ except Exception :
836+ element .scroll_into_view ()
819837 element .click () # Standard CDP click
820838 self .__slow_mode_pause_if_set ()
821839 self .loop .run_until_complete (self .page .wait (0.2 ))
@@ -3109,6 +3127,17 @@ def assert_not_in(self, first, second):
31093127 if first in second :
31103128 raise AssertionError ("%s is in %s" % (first , second ))
31113129
3130+ def js_scroll_into_view (self , selector ):
3131+ css_selector = self .__convert_to_css_if_xpath (selector )
3132+ css_selector = re .escape (css_selector ) # Add "\\" to special chars
3133+ css_selector = js_utils .escape_quotes_if_needed (css_selector )
3134+ js_code = (
3135+ "document.querySelector('%s')?.scrollIntoView();"
3136+ % css_selector
3137+ )
3138+ with suppress (Exception ):
3139+ self .loop .run_until_complete (self .page .evaluate (js_code ))
3140+
31123141 def scroll_into_view (self , selector ):
31133142 self .find_element (selector ).scroll_into_view ()
31143143 self .loop .run_until_complete (self .page .wait (0.1 ))
0 commit comments