0

I am getting invalid element state when I try to clear element after click.

Following are operations I am doing on element:

inputField.click();
inputField.clear();
inputField.sendKeys("name");

The first step click is working fine, but clear is giving exception:

org.openqa.selenium.InvalidElementStateException: invalid element state

There is another test case, which calls the method which has above three steps and it works fine.What can be potential issue?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
user124
  • 423
  • 2
  • 7
  • 26
  • The clear method is going set the value attribute to "" and then trigger an onchange. Include the HTML markup of the item you are targeting along with the selector. It could be there is no value, no type, or the type is wrong. – pcalkins Mar 29 '22 at 20:29

1 Answers1

0

InvalidElementStateException

InvalidElementStateException implies that a WebElement is in a certain state when actions cannot be performed with it. One of the most frequently encountered examples would include an element being obscured by another when clicking, or perhaps not being visible on the HTML DOM.


This usecase

As you are trying to click(), clear() and sendKeys() within the WebElement ideally you need to induce WebDriverWait for the elementToBeClickable() and you can use the following solution:

WebElement inputField = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//elementXpath")));
inputField.click();
inputField.clear();
inputField.sendKeys("name");
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • if this has to be the case, the why does click() works fine and error comes at clear() and not at click()? – user124 Mar 30 '22 at 06:04
  • @user124 You can click and even start typing within the Google Search Box even when the last few Ajax are still running to construct the DOM. Agree? – undetected Selenium Mar 30 '22 at 07:29