Skip to content

Commit fdee8e4

Browse files
committed
Add "focus(selector)" method
1 parent 1b95ae5 commit fdee8e4

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

help_docs/method_summary.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ self.submit(selector, by=By.CSS_SELECTOR)
3939

4040
self.clear(selector, by=By.CSS_SELECTOR, timeout=None)
4141

42+
self.focus(selector, by=By.CSS_SELECTOR, timeout=None)
43+
4244
self.refresh_page()
4345
# Duplicates: self.refresh(), self.reload(), self.reload_page()
4446

seleniumbase/fixtures/base_case.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,33 @@ def clear(self, selector, by=By.CSS_SELECTOR, timeout=None):
588588
except Exception:
589589
element.clear()
590590

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+
591618
def refresh_page(self):
592619
self.__check_scope()
593620
self.__last_page_load_url = None

0 commit comments

Comments
 (0)