Skip to content

Commit c5ec5fb

Browse files
committed
Add "self.clear(SELECTOR)"
1 parent 18422d3 commit c5ec5fb

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

help_docs/method_summary.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ self.add_text(selector, text, by=By.CSS_SELECTOR, timeout=None)
3333

3434
self.submit(selector, by=By.CSS_SELECTOR)
3535

36+
self.clear(selector, by=By.CSS_SELECTOR, timeout=None)
37+
3638
self.refresh_page()
3739
# Duplicates: self.refresh(), self.reload(), self.reload_page()
3840

seleniumbase/fixtures/base_case.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,46 @@ def submit(self, selector, by=By.CSS_SELECTOR):
441441
element.submit()
442442
self.__demo_mode_pause_if_active()
443443

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+
444484
def refresh_page(self):
445485
self.__last_page_load_url = None
446486
js_utils.clear_out_console_logs(self.driver)

0 commit comments

Comments
 (0)