java - 在java中使用selenium webdriver登录Gmail

标签 java selenium firefox geckodriver webdriverwait

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

关于java - 在java中使用selenium webdriver登录Gmail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57600990/

相关文章:

java - Maven 程序集 : DependencySet or ModuleSet

java - 在 Java 中将动态变量与线程一起使用

Selenium WebDriver 无需等待页面加载即可进入页面

xpath - 如何通过 Xpath 使用 Selenium 来确定元素的类?

c# - 如何在 Selenium C# 中使用现有的 Firefox 配置文件?

javascript - 从 URL 获取数据并防止自动重定向(附加 SDK Mozilla)

java - 戈朗 : what's the point of interfaces when you have multiple inheritence

Python 3.5 Selenium 网络驱动程序 IE

不同浏览器中的 CSS 悬停宽度样式

java - 如何在Java bean中存储Oracle Rowid