MCPcopy Index your code
hub / github.com/seleniumbase/SeleniumBase / focus

Method focus

seleniumbase/fixtures/base_case.py:1244–1282  ·  view source on GitHub ↗

Make the current page focus on an interactable element. If the element is not interactable, only scrolls to it. The "tab" key is another way of setting the page focus.

(self, selector, by="css selector", timeout=None)

Source from the content-addressed store, hash-verified

1242 element.clear()
1243
1244 def focus(self, selector, by="css selector", timeout=None):
1245 """Make the current page focus on an interactable element.
1246 If the element is not interactable, only scrolls to it.
1247 The "tab" key is another way of setting the page focus."""
1248 self.__check_scope()
1249 if not timeout:
1250 timeout = settings.LARGE_TIMEOUT
1251 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:
1252 timeout = self.__get_new_timeout(timeout)
1253 selector, by = self.__recalculate_selector(selector, by)
1254 if self.__is_cdp_swap_needed():
1255 self.cdp.focus(selector)
1256 return
1257 element = self.wait_for_element_present(
1258 selector, by=by, timeout=timeout
1259 )
1260 if not element.is_displayed():
1261 css_selector = self.convert_to_css_selector(selector, by=by)
1262 css_selector = re.escape(css_selector) # Add "\\" to special chars
1263 css_selector = self.__escape_quotes_if_needed(css_selector)
1264 script = """document.querySelector('%s').focus();""" % css_selector
1265 self.execute_script(script)
1266 self.__demo_mode_pause_if_active()
1267 return
1268 self.scroll_to(selector, by=by, timeout=timeout)
1269 try:
1270 element.send_keys(Keys.NULL)
1271 except (Stale_Exception, ENI_Exception):
1272 self.wait_for_ready_state_complete()
1273 time.sleep(0.12)
1274 element = self.wait_for_element_visible(
1275 selector, by=by, timeout=timeout
1276 )
1277 try:
1278 element.send_keys(Keys.NULL)
1279 except ENI_Exception:
1280 # Non-interactable element. Skip focus and continue.
1281 pass
1282 self.__demo_mode_pause_if_active()
1283
1284 def refresh_page(self, *args, **kwargs):
1285 self.__check_scope()

Callers

nothing calls this directly

Calls 14

__check_scopeMethod · 0.95
__get_new_timeoutMethod · 0.95
__is_cdp_swap_neededMethod · 0.95
execute_scriptMethod · 0.95
scroll_toMethod · 0.95

Tested by

no test coverage detected