3

I'm writing automation script for a page in which there is a text box. The scenario is as follows:

  • I click on the text box and enter a value which will throw error
  • I clear the text box and add valid value and click on confirm button
  • Then I see the answer provided in the text box as an answer bubble
  • I click on the edit button to change the answer bubble back into text box
  • I clear the text box and add new value

In the last step where I need to clear and add new value to the text box, what happens is that the text box is not cleared even when the code is there to clear it. In step 2 it does clear the text box.

 public void enterElectricityReading(String electricReading) {
    WebDriverWait wait = new WebDriverWait(driver, 60);
    wait.until(ExpectedConditions.elementToBeClickable(electricityField));
    electricityField.clear();
    electricityField.sendKeys(electricReading);
}

Can someone help me to understand why does the clear code is not run and value is not cleared?

At a certain period when the test is run then the clear method works. But it is not reliable.

7
  • Probably the fact that it becomes clickable does not automatically mean it becomes editable
    – Alexey R.
    Apr 20, 2018 at 13:41
  • Where and how is electricityField defined and initialized? Is this method inside a page object? Apr 20, 2018 at 13:46
  • @BillHileman Yes the method is inside the page object. It is initialized in the step definitions class. Apr 20, 2018 at 13:59
  • @AlexeyR. Could you please explain what you meant if possible. Apr 20, 2018 at 14:00
  • I would try creating a new instance of the page object before setting the value the second time. Apr 20, 2018 at 14:07

7 Answers 7

0

Check that when you click the button, the selector is still the same and/or it is still unique. It is possible that when you clicked the button, the html may have been changed by Javascript. The element you want to interact with may have a different selector.

1
  • The element is not changed. It is same. Apr 22, 2018 at 19:33
0
  • Try double clicking on the input instead of clearing. When you double click an input all the text gets selected, now when you sendkeys to it it will overwrite the text. I have used this in the past for a similar situation.
  • Try to write a clear loop, that clears, checks the value is empty, clears again, repeat for max X milliseconds.
0

You can use the following method to clear text fields, it is tested on different types of input fields

public boolean clearTextField(Object locator) {
    _element = _query.castLocator(locator);
    if (toBeCleared(_element)) {
        _element.clear();
    }
    if (toBeCleared(_element)) {
        _mouse.click(locator);
        // simulate select all text using `end`, `shift` and `home` keypresses.
        multipleKeyStrokes(locator, "end");
        multipleKeyStrokes(locator, "shift+home");
        multipleKeyStrokes(locator, "backspace");
    }
    return true;
}

private boolean toBeCleared(Object element) {
    boolean flag = true;

    String jText = (String) _js.executeScript("return $(arguments[0]).val();", element);
    String text = _query.getText(element);

    if (_verify.isStringMatch("", jText) && _verify.isStringMatch("", text)) {
        flag = false;
    }

    return flag;
}

public boolean multipleKeyStrokes(Object locator, String keys) {
    _element = _query.castLocator(locator);

    _browser.checkAndInjectSimulations();
    _js.executeScript("$(arguments[0]).simulate('key-combo', {combo: arguments[1]})", _element, keys);
    return true;
}

You can use Actions class to simulate keypresses, I have used javascript to do so in the multipleKeyStrokes() method.

0
driver.findelement(By.id("xyz").clear();

using above statement you can clear the text.

1
  • 2
    I'm not sure how you're able to find the ID from the given information - can you elaborate how this works in with the OP's question?
    – corsiKa
    Feb 13, 2020 at 20:33
0

I believe we are trying to complicate things where this could be handled easily by introducing some additional assertion intelligence.

Main issue is the selenium's clear method isn't clearing the text from the element.

So validating element is empty will provide you consistency.

Based on your default explicit wait, I have below suggestions hoping that would provide consistency you are required.

  1. Step 1 : Enter Text
  2. Step 2 : Validate Previously entered Text
  3. Step 3 : Clear the text using clear method.
  4. Step 4 : Validate element value is clear.
        username.sendKeys("Please work");
        
//Step 2 : Validate Previously entered Text
        wait.until(new ExpectedCondition<Boolean>() {
            public Boolean apply (WebDriver driver)
            {
                return driver.findElement(By.id("user-identifier-input")).getAttribute("value").contentEquals("Please work");
            }
        });

//Step 3 : Clear the text using clear method.
        username.clear();

//Step 4 : Validate element value is clear.
        wait.until(new ExpectedCondition<Boolean>() {
            public Boolean apply (WebDriver driver)
            {
                return driver.findElement(By.id("user-identifier-input")).getAttribute("value").isEmpty();
            }
        });
        
0

Can't send command+key to Chrome when using Mac OSX. So for clearing the text,Please use

send_keys(Keys.BACK_SPACE*50)

**Use this 50 or 100 to erase the text in any box. number represent the character length.

0

Working solutions for me :

  1. Click on exact[unique] textbox element before performing clear()
  2. Try to send empty string to textbox instead of performing clear() for textbox
electricityField.sendKeys("");
electricityField.sendKeys(electricReading);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.