clear() method doesn't clears the field in MS EDGE IE11 with Selenium-4.1.2

578 views Asked by At

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.

2

There are 2 answers

1
undetected Selenium On

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();
1
Marko Eronen On

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=''");