Selenium Chromedriver-正常打开Chrome [英] Selenium Chromedriver - Open Chrome Normally

查看:74
本文介绍了Selenium Chromedriver-正常打开Chrome的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to login to a web application using Selenium and ChromeDriver. I am able to fill in the email and password fields correctly but every time I click login it requires me to enter a new verification code sent to my email.

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

If I log in using Chrome normally it will skip this step. Is there a way to open Chrome using Selenium so that it remembers my usernames and passwords?

到目前为止,这是我的代码:

Here is my code so far:

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的示例:

Selenium uses a temporary browser profile. If you want to use existing profile, you need to specify it before driver opens the browser. An example for 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:

and for 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配置文件(不要求下载文件,不要求用户证书等).

selenium_profile is in my case customized firefox profile (don't ask to download files, don't ask for user certificate, etc.).

这篇关于Selenium Chromedriver-正常打开Chrome的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆