Skip to content Skip to sidebar Skip to footer

Sendkey() Doesnt Work In Selenium Webdriver

I tried to input phone numbers in the field but it gives me an error Exception in thread 'main' org.openqa.selenium.NoSuchElementException: no such element Here is the code:

Solution 1:

Exception org.openqa.selenium.NoSuchElementException tells the element not present on page when action is performed.

This may be because of either XPATH is not correct or element is not appeared on page before action is called.

Please run code in debug mode to find exact problem.

Here is quick example of google search box. I have put wrong id to make code fail. In this case I get the exception. If we correct the id "gbqfq" the code works fine.

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

public class GoogleSearchUsingSelenium {

    public static void main(String[] args) {

        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.google.com");

        try
        {
            WebElement searchBox = driver.findElement(By.id("gbqfq1")); //Incorrect ID here
            searchBox.sendKeys("Cheese");
        }
        catch(Exception e){
            System.out.println("Eelemnt Not Found : "+e.getMessage());
        }

        driver.close();

    }

}

Post a Comment for "Sendkey() Doesnt Work In Selenium Webdriver"