Erin Erin - 1 month ago 5
Java Question

Program terminates entering pass into textbox with webrunner

public static void main(String[] args) {


System.setProperty("webdriver.chrome.driver", "C://chromedriver//chromedriver.exe");
WebDriver driver = new ChromeDriver();

//Maximize the Browser window
driver.manage().window().maximize();

driver.get("https://google.com");

WebElement signin = driver.findElement(By.id("gb_70"));
signin.click();

WebElement username = driver.findElement(By.id("Email"));
username.sendKeys("email@gmail.com");

WebElement next = driver.findElement(By.id("next"));
next.click();

WebElement password = driver.findElement(By.id("Passwd"));
password.sendKeys("password");

WebElement next1 = driver.findElement(By.id("signIn"));
next1.click();


My program terminates when it gets to the password entry screen.. it says

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"Passwd"}
(Session info: chrome=53.0.2785.143)

However, I have tried ID element and Xpath element and they are both correct

:
(

Answer

Possibly you need to wait after next.click(); command. After clicking next button, your input filed is not loaded yet. So, wait for the element to get loaded and put text in it.

Edit:

You can use explicit wait for this. It's better way then Thread.sleep(). Specify a maximum time to wait for element. If the specified time elapsed before your element is visible then it will throw a exception.

Code snippet:

WebDriverWait wait = new WebDriverWait(driver, 30); // waiting for maxiumum of 30 seconds

wait.until(ExpectedConditions.presenceOfElementLocated(By.id("Password")));
Comments