0

I am getting this error while trying to write to simple code in selenium webdriver to enter a value in google search page and enter. Following is my code -:

    WebDriver driver = new FirefoxDriver(profile);
    driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
    driver.get("http://www.google.com");

    WebElement element=driver.findElement(By.xpath("//input[@id='gs_htif0']"));
        boolean b = element.isEnabled();

    if (b){

        System.out.println("Enabled");
    }

    element.sendKeys("Test Automation");

    element.submit();

Can anyone please help me out with this? How to enable a disabled element?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
user3134443
  • 1
  • 1
  • 1
  • 1

5 Answers5

1

You are using the wrong 'input' for entering the text. You should be using the following XPath:

//input[@name='q']

Like

WebElement element=driver.findElement(By.xpath("//input[@name='q']"));

This 'input' element accepts the input text just fine.

Vlad.Bachurin
  • 1,340
  • 1
  • 14
  • 22
0

You can try to run javascript on page:

((JavascriptExecutor) driver).executeScript("document.getElementById('gs_htif0').disabled = false");

or

((JavascriptExecutor) driver).executeScript("arguments[0].disabled = false", element);
Andrian Durlestean
  • 1,730
  • 1
  • 19
  • 30
  • Thanks Andrian the javascript worked. But I am still facing an issue. – user3134443 Dec 26 '13 at 17:52
  • Thanks Andrian the javascript worked. But I am still facing an issue. The function "sendKeys" is entering the text in not editable form and is not able to further click on Search button or using "submit()". – user3134443 Dec 26 '13 at 17:59
  • If you want to search something in google try to use id='gbqfq' or download Selenium IDE it will show correct input for google and SensKeys(Key.Enter) for start searching – Andrian Durlestean Dec 26 '13 at 19:51
0

See, if this might help,

WebDriver driver = new FirefoxDriver(profile);

driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);

driver.get("http://www.google.com");

WebElement element = driver.findElement(By.name("q"));

if(element.isEnabled()) {
    System.out.println("Enabled");
    element.sendKeys("Test Automation");
    element.submit();
}
0

Try this:

WebDriverWait wait = new WebDriverWait(driver, 40);
driver.get("http://www.google.com");

wait.until(ExpectedConditions.visibilityOfElementLocated(By
                .xpath("//input[@id='gs_htif0']")));
driver.findElement(By.xpath("//input[@id='gs_htif0']"))
                .sendKeys("Test Automation" + Keys.ENTER);

Or:

public boolean isElementPresent(WebDriver driver, By by)
{
try {
            driver.findElement(by);
            System.out.print("Enabled");
            return true;
          } catch (NoSuchElementException ignored) {  
            return false;
          }
}

isElementPresent = isElementPresent(
                driver, By.xpath("//input[@id='gs_htif0']"));
if (isElementPresent) { 
    element.sendKeys("Test Automation");
    element.submit();
 }

Or change xPath to name selector.

Milky
  • 143
  • 4
  • 15
0

If i am correct then you are using the firebug add-on in the firefox driver to get the path for the searchbox. But the firebug seems to provide a path where the Id for the searchbox is not correct. If you use the inspect element option you can see the difference (in the below image you can spot the difference yourself).

image.

Tonechas
  • 13,398
  • 16
  • 46
  • 80