1
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class NewGmail {
    public static void main(String[] args) {
            WebDriver driver = new FirefoxDriver();
            driver.manage().window().maximize();
            String url = "https://accounts.google.com/signin";
            driver.get(url);
            driver.findElement(By.id("identifierId")).sendKeys("cp8805"); 
            //driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);      
            WebDriverWait wait=new WebDriverWait(driver, 20);               
            driver.findElement(By.xpath("//span[@class='RveJvd snByac']")).click();         
            driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);        
            driver.findElement(By.xpath("//input[@class='whsOnd zHQkBf']")).sendKeys("xxxxxx");             
            driver.findElement(By.xpath("//span[@class='RveJvd snByac']")).click(); 
    }  
}

after mail id my password also get written in the id box option & the server redirect to to next password page. i want to ask what i will do so that my password would be entered only in password page.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
chandan
  • 67
  • 1
  • 1
  • 3

1 Answers1

14

Here is the working code block to login into your Gmail account through a valid set of credentials-

System.setProperty("webdriver.gecko.driver","C:\\your_directory\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
String url = "https://accounts.google.com/signin";
driver.get(url);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
WebElement email_phone = driver.findElement(By.xpath("//input[@id='identifierId']"));
email_phone.sendKeys("your_email_phone");
driver.findElement(By.id("identifierNext")).click();
WebElement password = driver.findElement(By.xpath("//input[@name='password']"));
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(password));
password.sendKeys("your_password");
driver.findElement(By.id("passwordNext")).click();

Update(5-Jan-2020)

Optimizing the above code block and adding a couple of arguments you can use:

public class browserAppDemo 
{
    public static void main(String[] args) throws Exception 
    {
        System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("start-maximized");
        options.setExperimentalOption("useAutomationExtension", false);
        options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
        WebDriver driver =  new ChromeDriver(options); 
        driver.get("https://accounts.google.com/signin")
        new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='identifierId']"))).sendKeys("emailID");
        driver.findElement(By.id("identifierNext")).click();
        new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@name='password']"))).sendKeys("password");
        driver.findElement(By.id("passwordNext")).click();
    }
}
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    How to login with Firefox? – shayanmalinda Jun 24 '20 at 06:48
  • 2
    Thanks for the details script sharing @DebanjanB. However, getting an error "Couldn't sign you in" after entering the Gmail Email id, we have functionality where otp would be sent to gmail, so need to login and read the email to get otp. Though added above Chromeoptions arguments, facing "Couldn't sign you in" issue. Please guide. – la1 Sep 10 '20 at 18:59
  • 1
    Same issue. Gmail block web driver. Any workaround? – eastwater Feb 18 '21 at 04:56