Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does WebElement.clear() Do to TextBoxes?

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?

like image 545
joshin4colours Avatar asked Mar 01 '13 21:03

joshin4colours


People also ask

How do you clear the content of a text box in Selenium?

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.

Which function sets the value property of the element to an empty string in Selenium?

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.

How do you overwrite the current input value in the editable field on page in 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.

How do I edit text box in Selenium?

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.


1 Answers

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()

like image 157
JimEvans Avatar answered Oct 12 '22 11:10

JimEvans