java - Selenium Chromedriver - 正常打开 Chrome

标签 java selenium selenium-chromedriver

我正在尝试使用SeleniumChromeDriver登录网络应用程序。我能够正确填写电子邮件和密码字段,但每次单击“登录”时,都要求我输入发送到我的电子邮件的新验证码。

如果我正常使用 Chrome 登录,它将跳过此步骤。有没有办法使用 Selenium 打开 Chrome,以便它记住我的用户名和密码?

这是迄今为止我的代码:

String baseUrl = "https://www.easports.com/fifa/ultimate-team/web-app/";
driver.get(baseUrl);
driver.manage().window().fullscreen();
Thread.sleep(10000);

WebElement login = driver.findElement(By.xpath("//*[@id='Login']/div/div/div[1]/div/button"));
login.click();
Thread.sleep(2000);



WebElement email = driver.findElement(By.xpath("//*[@id=\'email\']"));
email.sendKeys("******@hotmail.com");
Thread.sleep(1000);

WebElement password = driver.findElement(By.xpath("//*[@id='password']"));
password.sendKeys("*******");

WebElement loginButton = driver.findElement(By.xpath("//*[@id='btnLogin']/span"));
loginButton.click();
Thread.sleep(10000);

最佳答案

Selenium 使用临时浏览器配置文件。如果您想使用现有的配置文件,则需要在驱动程序打开浏览器之前指定它。 Chrome 的示例:

public class WebdriverSetup {   
    public static String chromedriverPath = "C:\\Users\\pburgr\\Desktop\\selenium-tests\\GCH_driver\\chromedriver.exe";

    // my default profile folder
    public static String chromeProfilePath = "C:\\Users\\pburgr\\AppData\\Local\\Google\\Chrome\\User Data";    

    public static WebDriver driver; 
    public static WebDriver startChromeWithCustomProfile() {
        System.setProperty("webdriver.chrome.driver", chromedriverPath);
        ChromeOptions options = new ChromeOptions();

        // loading Chrome with my existing profile instead of a temporary profile
        options.addArguments("user-data-dir=" + chromeProfilePath);

        driver = new ChromeDriver(options);
        driver.manage().window().maximize();
        return driver;
    }
    public static void shutdownChrome() {
        driver.close();
        driver.quit();
    }
}

对于 Firefox:

@BeforeClass
public static void setUpClass() {

    FirefoxOptions options = new FirefoxOptions();
    ProfilesIni allProfiles = new ProfilesIni();         
    FirefoxProfile selenium_profile = allProfiles.getProfile("selenium_profile");
    options.setProfile(selenium_profile);
    options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
    System.setProperty("webdriver.gecko.driver", "C:\\Users\\pburgr\\Desktop\\geckodriver-v0.20.0-win64\\geckodriver.exe");
    driver = new FirefoxDriver(options);
    driver.manage().window().maximize();

}

selenium_profile 在我的例子中是自定义的 Firefox 配置文件(不要求下载文件,不要求用户证书等)。

关于java - Selenium Chromedriver - 正常打开 Chrome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50562675/

相关文章:

Java-根据现有对象列表从流中获取新对象列表

java - 如何从处理程序获取数据

python - 使用 Selenium 从嵌套的 HTML 代码中识别元素(确认没有 iframe)

python - 使用 Selenium 上传文件失败

java - 使用 Java 验证 CSV 文件

java - 如何删除列表中的一堆对象

Selenium 测试在 Jenkins 运行时失败,但在命令行运行时成功

python - 以 root 身份运行时 Chromium webdriver 错误

java - 如何在 selenium webdriver 中将网络浏览器从 Firefox 更改为 Chrome/Opera/IE/Safari?

electron - Spectron 推出 10 个窗口; 10 次尝试后客户端初始化失败,挂起并失败