手动登录后如何使 Chrome Headless

2024-01-06

我正在测试我的网站。 我希望 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");

No,将无法让Chrome运行无头的手动登录后。

当您配置一个实例时Chrome驱动程序 using ChromeOptions() or DesiredCapabilities()在发起新的过程中Chrome 浏览会话配置得到baked进入铬驱动程序可执行并且将持续到该网络驱动程序和存在不可编辑的。所以你不能再添加任何东西Chrome选项 to the 网络驱动程序当前正在执行的实例。

即使你能够提取Chrome驱动程序 and Chrome会话属性例如会话ID, Cookies, 用户代理以及来自已启动的其他会话属性Chrome驱动程序 and Chrome 浏览会话您仍然无法更改属性集Chrome驱动程序.

更干净的方法是打电话driver.quit() within tearDown(){}方法close and destroy当前的Chrome驱动程序 and Chrome 浏览器优雅地实例化,然后跨越一组新的Chrome驱动程序 and Chrome 浏览器具有新配置集的实例。


tl; dr

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

  • 如何在同一会话中将 selenium webdriver 从无头模式设置为正常模式? https://stackoverflow.com/questions/54721676/how-to-set-selenium-webdriver-from-headless-mode-to-normal-mode-within-the-same/54722252#54722252
  • 更改现有网络驱动程序中的 ChromeOptions https://stackoverflow.com/questions/53023853/change-chromeoptions-in-an-existing-webdriver/53025100#53025100
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

手动登录后如何使 Chrome Headless 的相关文章

随机推荐