Selenium Chromedriver - Open Chrome Normally

后端 未结 1 943
鱼传尺愫
鱼传尺愫 2020-12-10 23:18

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

相关标签:
1条回答
  • 2020-12-11 00:03

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

    0 讨论(0)
提交回复
热议问题