Site icon Learn Automation

How To Solve IllegalStateException in Selenium WebDriver

Solve IllegalStateException in Selenium Webdriver

Today in this article, we will see What is illegalStateException and How to Solve illegalStateException in Selenium Webdriver.

This exception comes under unchecked exception in Java.

But in selenium usually working with Chrome browser we got this exception. While working with chrome when we set the incorrect path of chrome driver in System set property then we caught this exception or when we haven’t specify chrome driver path (directly creating Object of Chrome Driver class using  reference of WebDriver).

Below code generate IllegalStateException while using Chrome browser.

package datapkg;
import java.awt.AWTException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class ActionClassExp {
    
    
    public static void main(String[] args) throws AWTException, InterruptedException {
        
        //System.setProperty("webdriver.chrome.driver", "C:\\Users\\AJEET\\Downloads\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://automationtestings.com/");
        driver.manage().window().maximize();
        
        WebElement element = driver.findElement(By.xpath("//a[@href='https://automationtestings.com/category/interview-questions/']"));
        
        Actions a = new Actions(driver);
        a.moveToElement(element).click().perform();
    }
}

In above code I have commented System.setProperty line. Because of this it will throw illegalStateException.


How To solve illegalStateException in Selenium WebDriver

  1. Download chromedriver.exe from https://chromedriver.chromium.org/downloads
  2. Set the System Property for “chromedriver.exe” with chromedriver.exe path.
  3. setProperty(“webdriver.chrome.driver”, “here put path of chromedriver.exe”). See below syntax.

Syntax:

System.setProperty("webdriver.chrome.driver", "C:\\Users\\AJEET\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();

Full Code:

package datapkg;
import java.awt.AWTException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class ActionClassExp {
    
    
    public static void main(String[] args) throws AWTException, InterruptedException {

    System.setProperty("webdriver.chrome.driver", "C:\\Users\\AJEET\\Downloads\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://automationtestings.com/");
        driver.manage().window().maximize();
        
        WebElement element = driver.findElement(By.xpath("//a[@href='https://automationtestings.com/category/interview-questions/']"));
        
        Actions a = new Actions(driver);
        a.moveToElement(element).click().perform();
        
        
        
        

      }
}

WebDriver Exceptions in Selenium:

There are many exceptions occurs while using Selenium tool. Here we will discuss most common exceptions.

I have divided the exceptions in different category.

Not Found exception:

NoSuchWindowException: Webdriver trying to switch on an invalid window.

NoSuchFrameException: WebDriver trying to switch to an invalid frame.

NoSuchElementException: If we have given wrong locator or element is not present is DOM.

NoAlertPresentException: WebDriver trying to switch to an invalid alert.


TimeOutException: When the selenium takes longer time than the given wait time then it throws TimeOutException.

StaleElementReferenceException: If element is no longer present in the DOM due to page got refreshed or a frame/window switched or navigate to another page.


InvalidElementState Exception:

ElementNotVisibleException: If element is present but visibility is off or If synchronization issue between Selenium and web application or if we have used duplicate xpath then this exception is thrown.

ElementNotSelectableException:  if an element is disable (element is not clicked/selected) .


java.lang.illegalargumentexception in selenium

In Selenium, when we have given null or string length is zero in sendKeys() method the n it throw this exception.

Code:

package datapkg;

import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;



public class ElementNotInteractable {
    
      
         static WebDriver driver = null;
          public static void main(String[] args) throws InterruptedException {
            
            
            System.setProperty("webdriver.chrome.driver", "C:\\Users\\AJEET\\Downloads\\chromedriver_win32\\chromedriver.exe");
            driver = new ChromeDriver();
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            driver.get("https://www.google.com/");
            driver.findElement(By.name("q")).sendKeys();
            Thread.sleep(2000);
            driver.findElement(By.xpath("//div[@class='UUbT9']/div[2]/ul[1]//following::span[1]")).click();
            Assert.assertTrue("facebook - Google Search", true);
            
        }
        
    }


     



In above code you can see I passed empty value in sendKeys() method.


Summary

Exception handling is the very crucial part of every Selenium test script.

Above discussed exceptions are most common and you always faced these exceptions if you will do any mistake in selenium scripting.

Final word, Bookmark this post  ‘How To Solve IllegalStateException in Selenium WebDriver for future reference.

If you have other questions or feedback, the comment section is yours. Don’t forget to leave a comment below!

 

Must Read:

How To Solve ElementNotVisible Exception

How To Solve Element Not Interactable Exception

 

Exit mobile version