0

Recently, I have upgraded my Selenium version from 2.53 to 4.1.2 to enable testing of our application on MS EDGE IE11. But we are continuously facing issue when we are clearing any TextBox field using simple Selenium .clear(). Although, its clearing that respective and but also throwing below exception.

Did anyone else facing similar kind of issues with Selenium-4.1.2 ?

Exception:

org.openqa.selenium.InvalidElementStateException: A JavaScript error was encountered clearing the element. The driver assumes this is because the element is hidden, disabled or read-only, and it must not be to clear the element. Build info: version: '4.1.2', revision: '9a5a329c5a' System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_222' Driver info: org.openqa.selenium.ie.InternetExplorerDriver Command: [69d06b0b-b468-455b-9d3c-24626ad40e16, clearElement {id=2bb89cb8-5d24-4dd8-8e3a-be8fa7e1272e}] Capabilities {acceptInsecureCerts: false, browserName: internet explorer, browserVersion: 11, javascriptEnabled: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), se:ieOptions: {browserAttachTimeout: 0, elementScrollBehavior: 0, enablePersistentHover: true, ie.browserCommandLineSwitches: , ie.edgechromium: false, ie.edgepath: , ie.ensureCleanSession: false, ie.fileUploadDialogTimeout: 3000, ie.forceCreateProcessApi: false, ignoreProtectedModeSettings: false, ignoreZoomSetting: false, initialBrowserUrl: http://localhost:54726/, nativeEvents: true, requireWindowFocus: false}, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify} Element: [[InternetExplorerDriver: internet explorer on WINDOWS (69d06b0b-b468-455b-9d3c-24626ad40e16)] -> id: userid] Session ID: 69d06b0b-b468-455b-9d3c-24626ad40e16 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:200) at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:133) at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:53) at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:184) at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:167) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:142) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:558) at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:251) at org.openqa.selenium.remote.RemoteWebElement.clear(RemoteWebElement.java:126)

Expectation : We expect clear() should clear the field without throwing any exception.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I tried to reproduce this issue but failed. If possible, please provide more details, such as the version of `iedriver` and the simple code. I [tested with google](https://i.stack.imgur.com/T3wwl.png), and as you can see, it works fine. Whether this problem also occurs on other sites? – Xudong Peng Apr 06 '22 at 07:05
  • IEDriver Version : 4, I was testing on MS EDGE IE11 using Java-Selenium and Selenium Version : 4.1.2 – Gaurank Verma Apr 07 '22 at 11:02
  • You can see I tested with the same version, the only difference is the java version. If possible, please provide a code example that reproduces the problem, it will help to solve the problem. And as other members mentioned, you could try writing code to wait for the page element to load before `clear()`. – Xudong Peng Apr 08 '22 at 08:12

2 Answers2

1

This is due to a bug in the Edge IE mode driver https://github.com/SeleniumHQ/selenium/issues/10488

A possible workaround would be to use JavascriptExecutor to locate and clear the input field

JavascriptExecutor jsExec = (JavascriptExecutor)driver;
jsExec.executeScript("document.getElementById('field_to_clear').value=''");
0

InvalidElementStateException indicates that a WebElement is in a certain state in which actions cannot be performed with it. A couple of examples would include an element being obscured by another when clicking, or perhaps not being visible on the DOM.


Solution

It is always recommened that to clear any <input> field you need to induce WebDriverWait to elementToBeClickable() for the element (JavaScript) to render the HTML DOM completely before you invoke click() as follows:

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("element_cssSelector"))).clear();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • As senKeys() is working on that particular element, this indicates that HTML DOM got rendered properly before we performed .clear() operation. Our intension was to clear the textbox first then send the value, but unfortunately .clear() was NOT working. Hence, we tried sending values directly into that particular textbox and script was able to enter values properly. So, I think there should not be any rendering issue. – Gaurank Verma Apr 07 '22 at 11:14