3

I want to login by Selenium . It is divided the process into 2 pages.

  • email
  • password

Now I can input the key in first page .Then I should go next page (input password and click submit key).

However , If I just add 4 keys codes in one class ,it cannot complete the second page key input (password and submit )

I guess some code is missing between first page key input and second page key input.

public class Selenium {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {



WebDriver driver;
System.setProperty("webdriver.gecko.driver", "C:\\Users\\Downloads\\geckodriver-v0.10.0-win64\\wires.exe");
driver =new FirefoxDriver();


driver.get("https://mail.google.com");
driver.findElement(By.id("Email")).sendKeys("yourEmailId");//first page
driver.findElement(By.id("next")).click();//first page

driver.findElement(By.id("Passwd")).sendKeys("yourPassword");//next page
driver.findElement(By.id("signIn")).click();//next page
}

driver.get("https://mail.google.com");
driver.findElement(By.id("Email")).sendKeys("yourEmailId");//first page
driver.findElement(By.id("next")).click();//first page


/* What code should I add here?  */


driver.findElement(By.id("Passwd")).sendKeys("yourPassword");//next page
driver.findElement(By.id("signIn")).click();//next page
}
Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
Vito
  • 299
  • 3
  • 19

1 Answers1

2

Try setting an implicit wait of maybe 10 seconds before finding this element as :-

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.id("Passwd")).sendKeys("yourPassword");
driver.findElement(By.id("signIn")).click();

Or set an explicit wait. An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code. In your case, it is the visibility of the password input field.

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Passwd")));
element.sendKeys("yourPassword");

//Now click on sign in button 
driver.findElement(By.id("signIn")).click();//next page

Explanation: The reason selenium can't find the element is because the id of the password input field is initially Passwd-hidden. After you click on the "Next" button, Google first verifies the email address entered and then shows the password input field (by changing the id from Passwd-hidden to Passwd). So, when the password field is still hidden (i.e. Google is still verifying the email id), your webdriver starts searching for the password input field with id Passwd which is still hidden. And hence, you should wait until it becomes visible.

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • For the implicit wait , i add up to 30 sec . Sometimes it works and sometime doesn't work . – Vito Sep 22 '16 at 02:08
  • For the explicit wait , `WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Passwd"))).sendKeys("yourPassword");` incompatible types :void cannot be converted to WebEelment – Vito Sep 22 '16 at 02:10
  • @Vito Ah, sorry it just typo mistake, update it. Try now and let me know.. – Saurabh Gaur Sep 22 '16 at 03:06
  • explicit wait is fine .But for the implicit wait ,sometimes it works ,while sometime it doesnt .Can u explain y? – Vito Sep 22 '16 at 03:09
  • This could be timing issue, I would suggest you try always explicit wait because it's polls to locate element every 500ms while implicit wait polls only two time in the given time, that's may be problem, use explicit wait and get rid from this issue. Thanks.. – Saurabh Gaur Sep 22 '16 at 03:12
  • just one more question .What if the whole login process includes different stages and pages(different url).What property should i use to finish cross-pages login process ? – Vito Sep 22 '16 at 04:02
  • @Vito Sorry I didn't get you. Do you want to know about locator priority to locate elements?? – Saurabh Gaur Sep 22 '16 at 04:06
  • @Vito Follow this answer http://stackoverflow.com/questions/38716233/in-selenium-webdriver-which-is-better-in-terms-of-performance-linktext-or-css/38717398#38717398 – Saurabh Gaur Sep 22 '16 at 04:21