I've recently run into a problem working with selenium where calling clear()
on a custom text box causes issues when entering text later in the test. The text box does check for (JavaScript
) browserEvents
, particularly keyDown
events. I tried figuring out what clear()
does to see if that could be affecting things, but I can't seem to find any specifics.
The source for the Selenium Java bindings shows that clear()
does not use keyboard or mouse simulation to clear away the text from the text box. So what does it do, exactly?
We can enter text on any field in Selenium. After entering the text, we may need to remove or clear the text we entered in that field. This interaction with the web elements can be achieved with the help of clear() method. Thus a clear() method is used to clear or reset an input field belonging to a form/ edit box.
Here is what exactly clear() will do. The function will clear textbox value and enable text box. Before entering text in text field we need to clear the text field and will enable it. If we do not use clear () , we can't enter any value in text field using selenium.
To overwrite a value, we shall first select it with CTRL+A keys and then pass the new value. Thus, Keys. CONTROL, A and the new value are passed as parameters to the Keys.
Selenium can be used to input text to an edit box. An edit box is represented by the input tag and its type attribute should have the value as text. It can be identified with any of the locators like - id, class, name, css, xpath and tagname. To input a value into an edit box, we have to use the method send_keys.
The clear()
method executes an "Automation Atom", which is a JavaScript function intended to provide the smallest basic unit of automation functionality for a browser. In the case of clear()
, that function sets the value
property of the element to an empty string (''), then fires the onchange
event on the element. The atoms function you're interested in is bot.action.clear()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With