Solution for InvalidElementStateException in Selenium Webdriver

Recently I was working with some of the scenarios and I got InvalidElementStateException in Selenium Webdriver which was stopping me from performing any operation.

If you have worked with Selenium then I must say you also have faced InvalidElementStateException in Selenium Webdriver right?

If not then I will show you how you can get this exception and what will be the possible solution to handle this exception.

 

InvalidElementStateException in Selenium Webdriver

While working with Selenium Webdriver I have faced so many exceptions and trust me each exception was a new learning for me.

I have faced Stale element reference exception, Illegal state exception, Element is not clickable exception, Element is not visible exception, Jquery is not a function exception and list goes on.

In the last article ,we have already discussed how to handle the exception in Selenium but every exception can be handled in a different way.

I want to share some of the scenarios where you will get above exception.

  1. When we try to perform some operation which is not applicable then it will throw InvalidElementStateException.
  2. Let’s assume if the textbox is disabled and if you try to perform type operation then it will throw an exception.
  3. If radio button,checkbox or any other web element is disabled and if you try to perform click event then it will throw an exception.
  4. If any element supports only click events and if you try to perform type events then again it will throw an exception.

 

Let me show you how the InvalidElementStateException in Selenium Webdriver

In below image, if you see I have added disabled the password field

InvalidElementStateException in Selenium Webdriver

 

 

When we try to perform sendKeys then it will throw InvalidElementStateException in Selenium Webdriver.

 

InvalidElementStateException in Selenium Webdriver

Solution for InvalidElementStateException in Selenium Webdriver

 

  • Always try to perform the required operation based on element state.
  • If it is clickable element then perform click and if it supports type event then perform sendkeys.
  • If the element is disabled then  enable it first and then perform operations.

 

In our case, if we want to pass the data forcefully then we can do this using JavaScriptExecutor in Selenium Webdriver.

Syntax to enter data in Selenium without using sendKeys.

 

Program of InvalidElementStateException in Selenium Webdriver

 

I hope above article helped you to solve above exception.

If you have any other scenario where you might have faced above exception then feel free to let me know in the comment section.

If you have found this article then kindly share with your friends as well.

 

8 Comments

  1. asha
  2. jitheesh
  3. Rudragouda