1

I want to type 'calculator' on google text box so for that i wrote below code, its execute fine but there is 'InvalidElementStateException' message display so please help me how to solve that problem.

My code:

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Calculator_Excel {

    public static void main(String[] args) {

        Xls_Reader xl = new Xls_Reader("D:\\Calculator.xlsx");
        int rowcount = xl.getRowCount("Addition");
        int Colcount = xl.getColumnCount("Addition");

        System.out.println("Total Rows Are --- > " +rowcount);
        System.out.println("Total Columns Are ---> " +Colcount);

        WebDriver driver= new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://www.google.co.in");

        driver.findElement(By.xpath("//*[@id='sb_ifc0']")).sendKeys("calculator");
        driver.findElement(By.xpath("//*[@id='gs_htif0']")).sendKeys(Keys.ENTER);
        driver.findElement(By.xpath("//*[@id='sb_ifc0']")).clear();
        //for(int i =2;i<=rowcount;i++){
        //}
    }

}

Exception:-

Exception in thread "main" org.openqa.selenium.InvalidElementStateException: Element is disabled and so may not be used for actions
Command duration or timeout: 10.12 seconds
Build info: version: '2.51.0', revision: '1af067d', time: '2016-02-05 19:11:55'
System info: host: 'user-PC', ip: '192.168.1.70', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=46.0.1, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 0709d0c5-5893-4a25-b88e-42db93e77ad1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:327)
    at org.openqa.selenium.remote.RemoteWebElement.sendKeys(RemoteWebElement.java:122)
    at Calculator_Excel.main(Calculator_Excel.java:24)
Caused by: org.openqa.selenium.InvalidElementStateException: Element is disabled and so may not be used for actions
Build info: version: '2.51.0', revision: '1af067d', time: '2016-02-05 19:11:55'
System info: host: 'user-PC', ip: '192.168.1.70', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0'
Driver info: driver.version: unknown
    at <anonymous class>.fxdriver.preconditions.enabled(file:///C:/Users/user/AppData/Local/Temp/anonymous7563133565099206248webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:10051)
    at <anonymous class>.DelayedCommand.prototype.checkPreconditions_(file:///C:/Users/user/AppData/Local/Temp/anonymous7563133565099206248webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12585)
    at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/user/AppData/Local/Temp/anonymous7563133565099206248webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12602)
    at <anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///C:/Users/user/AppData/Local/Temp/anonymous7563133565099206248webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:623)
0

"//*[@id='sb_ifc0']" is the xpath of the div within which the search text field of Google is placed.

Replace the div's xpath with the xpath of the search text field and try to run your test. I think it should work.

Change,

driver.findElement(By.xpath("//*[@id='sb_ifc0']")).sendKeys("calculator");

To one of these,

driver.findElement(By.xpath(".//*[@id='gs_htif0']")).sendKeys("calculator");

driver.findElement(By.xpath(".//*[@id='lst-ib']")).sendKeys("calculator");

driver.findElement(By.name("q")).sendKeys("calculator");
0

Sumit,

The problem is happening because the sendKeys method must be used upon a input field.

The actions you must take are the following:

  1. Go to Google page
  2. Wait until the search bar (id:sb_ifc0) is visible (See this article)
  3. Click on the search bar (id:sb_ifc0) in order to enable the text input
  4. Fill in the search input (id:lst-ib)

It's always good to wait for visibility of the elements you are going to interact.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.