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

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

问题描述

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

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

我用来连接网站的 selenium 代码.

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

推荐答案

,手动登录后,Chrome 将无法无头操作.

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

当您在启动新 的过程中使用 ChromeOptions()DesiredCapabilities() 配置 ChromeDriver 的实例时Chrome 浏览会话配置被烘焙chromedriver可执行文件中,并将持续到WebDriver的生命周期并不可编辑.因此,您无法向当前正在执行的 WebDriver 实例添加任何进一步的 ChromeOptions.

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.

即使您能够提取 ChromeDriverChromeSession 属性,例如会话 IDCookiesUserAgent 和来自已启动的 ChromeDriverChrome 浏览会话的其他会话属性 您仍然无法更改 ChromeDriver 的属性集.

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.

更简洁的方法是在 tearDown(){} 方法中调用 driver.quit()关闭销毁当前的 ChromeDriverChrome Browser 实例,然后跨越一组新的 ChromeDriverChrome 浏览器具有新配置集的实例.

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 couple of relevant discussions in:

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

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