How do I make Chrome Headless after I login manual

2019-03-05 12:01发布

I am testing my site. I want the chrome browser to go headless after I manually enter the login credentials.

My selenium code to connect to the website.

    var driverService = ChromeDriverService.CreateDefaultService(); 
    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.AddArgument("--start-maximized");
    chromeOptions.AddArgument("no-sandbox");
    //chromeOptions.AddArgument("--headless");

    driver.Navigate().GoToUrl("exampleDOTcom");

    while (true)
    {
        Console.WriteLine("Login and Press Enter");
        Console.ReadLine();
        if (CheckLoggedIn())
            break;
    }
    //driver = new ChromeDriver(driverService, chromeOptions, 
    TimeSpan.FromSeconds(180));
    chromeOptions.AddArgument("--headless");

2条回答
干净又极端
2楼-- · 2019-03-05 12:11

If you need headless browser, Please Specify before the browser opening. i Haven't Seen any applications doing that activity

Things to pay attention to before using headless browser:

Headless browsers are simulation programs, they are not your real browsers. Most of these headless browsers have evolved enough to simulate, to a pretty close approximation, like a real browser. Still you would not want to run all your tests in a headless browser. JavaScript is one area where you would want to be really careful before using a Headless browser. JavaScript are implemented differently by different browsers. Although JavaScript is a standard but each browser has its own little differences in the way that they have implemented JavaScript. This is also true in case of headless browsers also. For example HtmlUnit headless browser uses the Rihno JavaScript engine which not being used by any other browser.

Here is Article = https://developers.google.com/web/updates/2017/04/headless-chrome

Check it out!

查看更多
疯言疯语
3楼-- · 2019-03-05 12:14

No, it won't be possible to make Chrome operate headlessly after you login manually.

When you configure an instance of a ChromeDriver with ChromeOptions to span a new Chrome Browsing Session the configuration of the ChromeDriver remains unchanged throughout the lifetime of the ChromeDriver and remains uneditable. So you can't add any further ChromeOptions to the WebDriver instance which is currently in execution.

Even if you are able to extract the ChromeDriver and ChromeSession attributes e.g. Session ID, Cookies, UserAgent and other session attributes from the already initiated ChromeDriver and Chrome Browsing Session still you won't be able to change the set of attributes of the ChromeDriver.

A cleaner way would be to call driver.quit() within tearDown(){} method to close and destroy the current ChromeDriver and Chrome Browser instances gracefully and then span a new set of ChromeDriver and Chrome Browser instance with the new set of configurations.

You can find a relevant discussion in How can I reconnect to the browser opened by webdriver with selenium?

查看更多
登录 后发表回答