手动登录后如何使Chrome无头 [英] How do I make Chrome Headless after I login manually

查看:89
本文介绍了手动登录后如何使Chrome无头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试我的网站. 手动输入登录凭据后,我希望chrome浏览器变得无脑.

我要连接到网站的硒代码.

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

解决方案

,您手动登录后将无法使Chrome自动无头运行.

在启动新的 Chrome浏览器会话的过程中使用ChromeOptions()DesiredCapabilities()配置 ChromeDriver 的实例时,配置被烘焙放入 chromedriver 可执行文件中,并将一直保留到 WebDriver 的生存期并且不可编辑.因此,您无法将任何其他 ChromeOptions 添加到当前正在执行的 WebDriver 实例中.

即使您能够提取 ChromeDriver ChromeSession 属性,例如会话ID Cookies UserAgent 和来自已启动的 ChromeDriver Chrome浏览器会话的其他会话属性仍然无法更改 ChromeDriver 的属性集.

一种更干净的方法是在tearDown(){}方法中调用 driver.quit() 关闭销毁当前的 ChromeDriver Chrome浏览器实例,然后使用新的配置集跨一组新的 ChromeDriver Chrome浏览器实例.


tl;博士

您可以在以下位置找到一些相关的讨论:

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

解决方案

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

When you configure an instance of a ChromeDriver using ChromeOptions() or DesiredCapabilities() in the process of initiating a new Chrome Browsing Session the configuration gets baked into the chromedriver executable and will persist till the lifetime of the WebDriver and being 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.


tl; dr

You can find a couple of relevant discussions in:

这篇关于手动登录后如何使Chrome无头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆