Cannot enter text in the search text box of Google.com using Selenium web driver

Currently with the new version of googlle.com I am not able to enter the text into the google search box as it is currently disabled. Once we point out the cursor on to the google search box then only the search parameter can be entered. But how can I set focus into the search text box using Selenium web driver?


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

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

search_textbox.sendKeys("Selenium Automation");

driver.findElement(By.xpath(".//*[@id='sblsbb']/button")).click();

Getting error message as

Exception in thread “main” org.openqa.selenium.InvalidElementStateException: Element is disabled and so may not be used for actions

Train Asked on October 25, 2015 in Selenium WebDriver.
Add Comment
  • 4 Answer(s)

    Hi KG,

    Have you tried clicking first the search_textbox before using sendKeys method?

     

     
    
    
    driver.get("http://www.google.com");
    WebElement search_textbox = driver.findElement(By.xpath(".//*[@id='gs_htif0']"));
    search_textbox.click(); /// click first the textbox, so the cursor will focus on the textbox..
    search_textbox.sendKeys("Selenium Automation");
    driver.findElement(By.xpath(".//*[@id='sblsbb']/button")).click();
     

     

    Supporter Answered on October 26, 2015.
    Add Comment

    I tried by clicking the text box  search_textbox.click(); before using the sendkeys method but displayed the same error message
    Exception in thread “main” org.openqa.selenium.InvalidElementStateException: Element is disabled and so may not be used for actions .

    The thing is when the google page is opened up using selenium web driver the text box is getting disabled but when you open up manually without using selenium the cursor is already placed in the text box and it is enabled.

    Not sure how to set the focus in the search text box.

    Train Answered on October 26, 2015.

    You have selected the wrong element try this and it should be working fine..
    WebElement search_textbox = driver.findElement(By
    .xpath(“.//input[@name=’q’]”));

    on October 26, 2015.
    Add Comment

    But why it is showing up a different xpath for me for the search text box? Please see the attached screenshot

    RE: Cannot enter text in the search text box of Google.com using Selenium web driver

    Train Answered on October 26, 2015.

    Honestly you cannot always depend on the firepath tool.  I suggest when looking for the right element, always try the id,className and Name of the element first before using Xpath. For me its always trial and error when looking for the effective element or Xpath..

    on October 26, 2015.
    Add Comment

    OK thanks for your advise.

    Train Answered on October 26, 2015.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.