@@ -588,6 +588,33 @@ def clear(self, selector, by=By.CSS_SELECTOR, timeout=None):
588
588
except Exception :
589
589
element .clear ()
590
590
591
+ def focus (self , selector , by = By .CSS_SELECTOR , timeout = None ):
592
+ """ Make the current page focus on an interactable element.
593
+ If the element is not interactable, only scrolls to it.
594
+ The "tab" key is another way of setting the page focus. """
595
+ self .__check_scope ()
596
+ if not timeout :
597
+ timeout = settings .LARGE_TIMEOUT
598
+ if self .timeout_multiplier and timeout == settings .LARGE_TIMEOUT :
599
+ timeout = self .__get_new_timeout (timeout )
600
+ selector , by = self .__recalculate_selector (selector , by )
601
+ element = self .wait_for_element_visible (
602
+ selector , by = by , timeout = timeout )
603
+ self .scroll_to (selector , by = by , timeout = timeout )
604
+ try :
605
+ element .send_keys (Keys .NULL )
606
+ except (StaleElementReferenceException , ENI_Exception ):
607
+ self .wait_for_ready_state_complete ()
608
+ time .sleep (0.12 )
609
+ element = self .wait_for_element_visible (
610
+ selector , by = by , timeout = timeout )
611
+ try :
612
+ element .send_keys (Keys .NULL )
613
+ except ENI_Exception :
614
+ # Non-interactable element. Skip focus and continue.
615
+ pass
616
+ self .__demo_mode_pause_if_active ()
617
+
591
618
def refresh_page (self ):
592
619
self .__check_scope ()
593
620
self .__last_page_load_url = None
0 commit comments