2

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.

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);
Neuron
  • 5,141
  • 5
  • 38
  • 59
user2958268
  • 67
  • 1
  • 4

1 Answers1

3

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

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 is in my case customized firefox profile (don't ask to download files, don't ask for user certificate, etc.).

pburgr
  • 1,722
  • 1
  • 11
  • 26
  • can you provide an example of how selenium_profile should be built and also what it should contains to avoid this confermation code procedure? – TheKill-996 Nov 27 '20 at 23:43
  • Just proceed the login process manualy. If browser (better say browser profile) is set to remember cookies etc., launching selenium with this profile should be enough. Ofc it is possible to create a new browser profile which will be dedicated to selenium tests. Above that the browser profile folder can be included in project and it's build, useful when running in dockered windows/linux environments (usualy hooked-up with CI like jenkins). – pburgr Nov 30 '20 at 07:07