【问题标题】:Selenium Chromedriver - Open Chrome NormallySelenium Chromedriver - 正常打开 Chrome
【发布时间】:2019-06-24 14:27:20
【问题描述】:

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

如果我使用 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);

【问题讨论】:

    标签: java selenium selenium-chromedriver


    【解决方案1】:

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

    【讨论】:

    • 您能否提供一个示例,说明应该如何构建 selenium_profile 以及它应该包含什么以避免这种确认代码过程?
    • 只需手动进行登录过程。如果浏览器(最好是浏览器配置文件)设置为记住 cookie 等,使用此配置文件启动 selenium 就足够了。 Ofc 可以创建一个专门用于 selenium 测试的新浏览器配置文件。除此之外,浏览器配置文件夹可以包含在项目中并构建,在 dockered windows/linux 环境中运行时很有用(通常与 jenkins 等 CI 挂钩)。
    猜你喜欢
    • 1970-01-01
    • 2022-12-24
    • 2020-06-22
    • 2018-12-18
    • 2016-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多