在java中使用selenium webdriver登录Gmail

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44455269/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:32:50  来源:igfitidea点击:

Gmail login using selenium webdriver in java

javaseleniumfirefoxgeckodriver

提问by chandan

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.

在邮件 id 之后我的密码也被写入 id 框选项 & 服务器重定向到下一个密码页面。我想问一下我会怎么做才能只在密码页面中输入我的密码。

回答by DebanjanB

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

这是通过一组有效的凭据登录到您的 Gmail 帐户的工作代码块 -

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)

更新(2020 年 1 月 5 日)

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();
    }
}