在java中使用Selenium网络驱动程序进行Gmail登录


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

邮件ID之后,我的密码也会写在ID框选项中,服务器重定向到下一个密码页面。 我想问我将怎么做,以便我的密码仅在密码页面中输入。

这是通过一组有效的凭据登录您的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();

更新(2020年1月5日(

优化上面的代码块并添加几个您可以使用的参数:

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

最新更新