Selenium Chromedriver - нормально открытый хром - PullRequest
0 голосов
/ 28 мая 2018

Я пытаюсь войти в веб-приложение, используя Selenium и ChromeDriver.Я могу правильно заполнить поля электронной почты и пароля, но каждый раз, когда я нажимаю на кнопку «Войти», мне необходимо ввести новый проверочный код, отправленный на мою электронную почту.

Если я войду в систему с помощью Chrome, то этот шаг пропустится.Есть ли способ открыть Chrome с помощью Selenium, чтобы он запоминал мои имена пользователей и пароли?

Вот мой код:

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

1 Ответ

0 голосов
/ 28 мая 2018

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 (не просить загружать файлы, не просить пользователясертификат и т. д.).

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...