@@ -441,6 +441,46 @@ def submit(self, selector, by=By.CSS_SELECTOR):
441
441
element .submit ()
442
442
self .__demo_mode_pause_if_active ()
443
443
444
+ def clear (self , selector , by = By .CSS_SELECTOR , timeout = None ):
445
+ """ This method clears an element's text field.
446
+ A clear() is already included with most methods that type text,
447
+ such as self.type(), self.update_text(), etc.
448
+ Does not use Demo Mode highlights, mainly because we expect
449
+ that some users will be calling an unnecessary clear() before
450
+ calling a method that already includes clear() as part of it.
451
+ In case websites trigger an autofill after clearing a field,
452
+ add backspaces to make sure autofill doesn't undo the clear.
453
+ @Params
454
+ selector - the selector of the text field
455
+ by - the type of selector to search by (Default: CSS Selector)
456
+ timeout - how long to wait for the selector to be visible
457
+ """
458
+ if not timeout :
459
+ timeout = settings .LARGE_TIMEOUT
460
+ if self .timeout_multiplier and timeout == settings .LARGE_TIMEOUT :
461
+ timeout = self .__get_new_timeout (timeout )
462
+ selector , by = self .__recalculate_selector (selector , by )
463
+ element = self .wait_for_element_visible (
464
+ selector , by = by , timeout = timeout )
465
+ self .scroll_to (selector , by = by , timeout = timeout )
466
+ try :
467
+ element .clear ()
468
+ backspaces = Keys .BACK_SPACE * 42 # Autofill Defense
469
+ element .send_keys (backspaces )
470
+ except (StaleElementReferenceException , ENI_Exception ):
471
+ self .wait_for_ready_state_complete ()
472
+ time .sleep (0.06 )
473
+ element = self .wait_for_element_visible (
474
+ selector , by = by , timeout = timeout )
475
+ element .clear ()
476
+ try :
477
+ backspaces = Keys .BACK_SPACE * 42 # Autofill Defense
478
+ element .send_keys (backspaces )
479
+ except Exception :
480
+ pass
481
+ except Exception :
482
+ element .clear ()
483
+
444
484
def refresh_page (self ):
445
485
self .__last_page_load_url = None
446
486
js_utils .clear_out_console_logs (self .driver )
0 commit comments