title: Selenium : How to solve org.openqa.selenium.InvalidElementStateException: invalid element state
body: <p>I have read a few online articles and they all point to 1 direction and that is when the page is loading the element is not found.  In my setData() you can see I have tried a few things such as use of wait, implicitwait and 1st click and then send the username. However nothing seems to be working. I also thought of using "wait" inside pageProperties but later changed my mind because that would probably be a bad design. </p>
  
  <p>Error : </p>
  
  <pre><code>Exception in thread "main" org.openqa.selenium.InvalidElementStateException: invalid element state
  </code></pre>
  
  <p>System information: Windows 10, ChromeDriver 2.37.544315, chrome=65.0.3</p>
  
  <p><strong>Code</strong>:</p>
  
  <pre><code>public class Tour {
  
      public static WebDriver driver;
  
      //browser URL information
      public WebDriver getBrowser(String browser, String url){
          System.setProperty("webdriver.chrome.driver", "C:\\Users\\Downloads\\chromedriver.exe");
  
          if(browser.equals("cc")){
              driver= new ChromeDriver();
          }
          driver.get(url);
          return driver;
      }
  
      // User name and continue button property from 1st page
      public void pageUserNameProperty(String un){
          WebElement login=driver.findElement(By.xpath("//input[@id='usernameOrEmail']"));
          WebElement cont_btn=driver.findElement(By.xpath("//button[contains(@type,'submit')]"));
  
          //WebDriverWait wait = new WebDriverWait(driver,30);
          //wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='usernameOrEmail']")));
          login.sendKeys(un);
          cont_btn.click();
  
      }
      // Password and continue button property from 2nd page
      public void pagePassTourProperty(String psd){
          WebElement password=driver.findElement(By.xpath("//input[@id='password']"));
          WebElement lgn_btn=driver.findElement(By.xpath("//button[contains(@type,'submit')]"));
  
          //WebDriverWait wait = new WebDriverWait(driver,30);
          //wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='usernameOrEmail']")));
          password.sendKeys(psd);
          lgn_btn.click();
  
      }
  
      // Supply Data for test from excel
      public void setData(){
          Tour tour= new Tour();
          tour.getBrowser("cc", "https://wordpress.com/log-in");
          WebDriverWait wait = new WebDriverWait(driver,30);
          WebElement login_field=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='usernameOrEmail']")));
          login_field.click();
          //driver.manage().timeouts().implicitlyWait(18, TimeUnit.SECONDS);
          tour.pageUserNameProperty("JoeThomas");
          //wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='passTour']")));
          driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
          tour.pagePassTourProperty("psd231");
      }
  
      public static void main(String[] args) {
          Tour tour= new Tour();
          tour.setData();
  
      }
  
  }
  </code></pre>
  
  <p>Error: </p>
  
  <pre><code>Starting ChromeDriver 2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7) on port 2644 
  Only local connections are allowed. 
  Apr 18, 2018 7:09:26 AM org.openqa.selenium.remote.ProtocolHandshake createSession 
  INFO: Detected dialect: OSS 
  Exception in thread "main" org.openqa.selenium.InvalidElementStateException: invalid element state   (Session info: chrome=65.0.3325.181)   
  (Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Windows NT 10.0.15063 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 0 milliseconds Build info: version: '3.11.0', revision: 'e59cfb3', time: '2018-03-11T20:33:15.31Z' System info: host: 'XYZ', ip: '123', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_91' 
  Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 2.37.544315 (730aa6a5fdba15..., userDataDir: C:\Users\CHQ-SH~1\AppData\L...}, cssSelectorsEnabled: true, databaseEnabled: false, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, rotatable: false, setWindowRect: true, takesHeapSnapshot: true, takesScreenshot: true, unexpectedAlertBehaviour: , unhandledPromptBehavior: , version: 65.0.3325.181, webStorageEnabled: true} 
  Session ID: 3298d88e517d756790ab6792e45257f1    
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)    
  </code></pre>
  
  <p>Thank You for your time and suggestion. </p>
  
  <p><strong>Experiment:</strong></p>
  
  <pre><code>public class Tour {
  
      public static WebDriver driver;
  
      //browser URL information
      public WebDriver getBrowser(String browser, String url){
          System.setProperty("webdriver.chrome.driver", "C:\\Users\\chq-sheikhr\\Downloads\\chromedriver.exe");
  
          if(browser.equals("cc")){
              driver= new ChromeDriver();
          }
          driver.get(url);
          return driver;
      }
  
      // User name and continue button property from 1st page
      public void pageUserNameProperty(String un){
          WebElement login=driver.findElement(By.xpath("//input[@id='usernameOrEmail']"));
          WebElement cont_btn=driver.findElement(By.xpath("//button[contains(@type,'submit')]"));
  
          login.sendKeys(un);
          cont_btn.click();
  
      }
      // Password and continue button property from 2nd page
      public void pagePasswordProperty(String psd){
          WebElement password=driver.findElement(By.xpath("//input[@id='password']"));
          WebElement lgn_btn=driver.findElement(By.xpath("//button[contains(@type,'submit')]"));
  
          password.sendKeys(psd);
          lgn_btn.click();
      }
  
      // A method - isElementExists to check whether that element exists or not
      public boolean isElementExists(By xpath){
          return driver.findElements(By.xpath("//input[@id='usernameOrEmail']")).size() &gt; 0;
      }
  
      /*create a method waitForElement and pass wait time in seconds to it, it is not a hard code wait as it
       *  will continuously check whether that element is exist or not and then it will wait for 1 seconds on 
       *  every iteration of for loop
       */
      public boolean waitForElement(int timeInSeconds, By xpath){
          try{
              for(int i=0;i&lt;timeInSeconds;i++){
                  if(isElementExists(xpath))
                      return true;
                  Thread.sleep(1000);
              }
          }catch(Exception ex){
              ex.printStackTrace();
          }
          return false;
      }
  
      public void setData(){
          Tour tour= new Tour();
          tour.getBrowser("cc", "https://wordpress.com/log-in");
          tour.waitForElement(10, By.xpath("//input[@id='usernameOrEmail']"));
          tour.pageUserNameProperty("JoeThoman");
          tour.pagePasswordProperty("pasd123");
  
  
      }
  
      public static void main(String[] args) {
          Tour tour= new Tour();
          tour.setData();
      }
  
  }
  </code></pre>