widgetastic.core
widgetastic.core copied to clipboard
clear() does not work reliably
When using chrome browser in selenium, calls to TextInput.fill("")
to clear a textarea element does not work. Looking at the browser console in the selenium container, I can see that after fill
calls clear()
on the textarea element, the element in the DOM still has the original user-entered text as its contents, even though the browser is displaying the placeholder text and el.get_attribute("value")
returns an empty string.
Relevant code:
widgetastic/widget/input.py:
class TextInput(BaseInput):
[...]
def fill(self, value):
[...]
self.browser.click(self)
self.browser.clear(self)
self.browser.send_keys(value, self)
return True
widgetastic/browser.py:
def clear(self, locator: LocatorAlias, *args, **kwargs) -> None:
"""Clears a text input with iven locator."""
self.logger.debug("clear: %r", locator)
el = self.element(locator, *args, **kwargs)
self.plugin.before_keyboard_input(el, None)
result = el.clear()
if el.get_attribute("value") and self.browser_type == "chrome":
# Chrome is not able to clear input with element.clear() method, use javascript instead
# We need to click on element
el.click()
self.execute_script("arguments[0].value = '';", el)
# If clearing is not followed by send_keys, the previous text will appear again
el.send_keys(Keys.SPACE, Keys.BACK_SPACE)
self.plugin.after_keyboard_input(el, None)
return result
From https://github.com/webdriverio/webdriverio/discussions/7650 it sounds like using the clear()
WebDriver method does not work well when the element is controlled by React. Because el.get_attribute("value")
returns an empty string, the workaround code in clear()
for chrome doesn't run.
Instead of using clear()
, it might be better (and closer to what a user would do) to use send_keys()
to clear the text (e.g., with control-X or backspace * the length of the current value) instead of clear()
.