How to make the Selenium wait until the next page loads up?

885 views Asked by At

Wait for page load in Selenium

I am new to coding and Automation testing. All I know in coding is if-else, while, do while and for, etc. Minor stuffs like that. The above is the link which I tried to checkout to make Selenium wait for a while, but couldn't make it happen. The below is the following code that I have written in order to login to my gmail account but isn't working. Can anyone correct the code? I think I need to delay the action of selenium and let the page loads first.

public class loginGmail {

    public static void main(String[] args) 
    {
        System.setProperty("webdriver.gecko.driver", "E:\\eclipse\\Drivers\\geckodriver.exe");
        FirefoxDriver d = new FirefoxDriver();
        d.get("https://www.gmail.com");
        d.findElement(By.id("Email"));
        WebElement email = d.findElement(By.id("Email"));
        email.sendKeys("[email protected]");

        d.findElement(By.name("signIn"));
        WebElement next = d.findElement(By.name("signIn"));
        next.click();



        d.findElement(By.id("Passwd"));
        WebElement pass = d.findElement(By.id("Passwd"));
        pass.sendKeys("********");

        d.findElement(By.id("signIn"));
        WebElement signIn = d.findElement(By.id("signIn"));
        signIn.click();
    } 
}
2

There are 2 answers

0
Lucas Tierney On BEST ANSWER

This is a known bug in geckodriver. To get around it for now, use explicit waits

0
JeffC On

I don't have a gmail account so I can't test this but this should work. I wouldn't have thought that you would need to wait for the initial page load but maybe you do. I added a wait at the start and removed a lot of unnecessary code. You were finding each element twice. Also, if you don't need the element for more than one thing (click, sendKeys, etc.) then there's no need to store it in a variable. Just use it in a one liner. Also, call your driver driver... no one knows what d is. You want to name your variables so that it's clear what they contain. Saving a few keystrokes that makes something less clear isn't saving time and with modern IDEs, you can just use intellisense-like features to autocomplete the driver name with a keypress.

new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.id("Email")))
        .sendKeys("[email protected]");
// driver.findElement(By.name("signIn")).click(); // don't think this is needed?
driver.findElement(By.id("Passwd")).sendKeys("********");
driver.findElement(By.id("signIn")).click();