MCPcopy Create free account
hub / github.com/seleniumbase/SeleniumBase / clear

Method clear

seleniumbase/fixtures/base_case.py:1198–1242  ·  view source on GitHub ↗

This method clears an element's text field. A clear() is already included with most methods that type text, such as self.type(), self.update_text(), etc. Does not use Demo Mode highlights, mainly because we expect that some users will be calling an unnecessary clear()

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

Source from the content-addressed store, hash-verified

1196 self.__demo_mode_pause_if_active()
1197
1198 def clear(self, selector, by="css selector", timeout=None):
1199 """This method clears an element's text field.
1200 A clear() is already included with most methods that type text,
1201 such as self.type(), self.update_text(), etc.
1202 Does not use Demo Mode highlights, mainly because we expect
1203 that some users will be calling an unnecessary clear() before
1204 calling a method that already includes clear() as part of it.
1205 In case websites trigger an autofill after clearing a field,
1206 add backspaces to make sure autofill doesn't undo the clear.
1207 @Params
1208 selector - The selector of the text field.
1209 by - The type of selector to search by. (Default: "css selector")
1210 timeout - How long to wait for the selector to be visible."""
1211 self.__check_scope()
1212 if not timeout:
1213 timeout = settings.LARGE_TIMEOUT
1214 if self.timeout_multiplier and timeout == settings.LARGE_TIMEOUT:
1215 timeout = self.__get_new_timeout(timeout)
1216 selector, by = self.__recalculate_selector(selector, by)
1217 if self.__is_cdp_swap_needed():
1218 self.cdp.clear_input(selector)
1219 return
1220 if self.__is_shadow_selector(selector):
1221 self.__shadow_clear(selector, timeout)
1222 return
1223 element = self.wait_for_element_visible(
1224 selector, by=by, timeout=timeout
1225 )
1226 self.scroll_to(selector, by=by, timeout=timeout)
1227 try:
1228 element.clear()
1229 backspaces = Keys.BACK_SPACE * 42 # Autofill Defense
1230 element.send_keys(backspaces)
1231 except (Stale_Exception, ENI_Exception):
1232 self.wait_for_ready_state_complete()
1233 time.sleep(0.16)
1234 element = self.wait_for_element_visible(
1235 selector, by=by, timeout=timeout
1236 )
1237 element.clear()
1238 with suppress(Exception):
1239 backspaces = Keys.BACK_SPACE * 42 # Autofill Defense
1240 element.send_keys(backspaces)
1241 except Exception:
1242 element.clear()
1243
1244 def focus(self, selector, by="css selector", timeout=None):
1245 """Make the current page focus on an interactable element.

Callers 15

clear_text_fieldFunction · 0.45
clear_cookiesMethod · 0.45
wissenMethod · 0.45
effacerMethod · 0.45
cancellareMethod · 0.45
despejarMethod · 0.45
지우려면Method · 0.45
очиститьMethod · 0.45
クリアするMethod · 0.45
清除Method · 0.45
limparMethod · 0.45
update_textMethod · 0.45

Calls 12

__check_scopeMethod · 0.95
__get_new_timeoutMethod · 0.95
__is_cdp_swap_neededMethod · 0.95
__is_shadow_selectorMethod · 0.95
__shadow_clearMethod · 0.95
scroll_toMethod · 0.95
clear_inputMethod · 0.80
send_keysMethod · 0.45
sleepMethod · 0.45

Tested by 3

test_add_item_to_cartMethod · 0.36
test_add_item_to_cartMethod · 0.36
test_add_item_to_cartMethod · 0.36