首页 > 解决方案 > Selenium 禁用恢复页面弹出窗口

问题描述

我正在使用 selenium C#,我正在尝试禁用“崩溃”chrome 的弹出窗口: https://i.stack.imgur.com/xudon.png

我试图设置配置文件首选项,但似乎它根本没有改变,代码:

        ChromeOptions options = new ChromeOptions();
        options.AddUserProfilePreference("exit_type", "Normal");
        options.AddUserProfilePreference("exited_cleanly", "true");
        IWebDriver driver = new ChromeDriver(options);

我尝试将退出类型的值更改为无和无,但在首选项文档中没有任何更改。

标签: c#google-chromeseleniumselenium-chromedriver

解决方案


我正在使用 C#,我注意到只有在 finally 块中使用 Close() 方法后跟 Quit() 时才能关闭 Chrome 驱动程序。不需要特殊选项。我认为在java中它是相同的方式。这将有助于在使用驱动程序启动 chrome 时摆脱“恢复页面”

ChromeOptions options = new ChromeOptions();
options.AddArgument(Configure.chromeProfileDir);
options.AddArgument(Configure.chromePath);

ChromeDriver d = null;

try
{
    d = new ChromeDriver(options);
    d.Navigate().GoToUrl("https://google.com");

    // Your operations...
}
catch(Exception e)
{
    // Handle your exceptions...
}
finally 
{
    try 
    {
        d.Close();
        d.Quit();
    } 
    catch(Exception e) 
    {
    }
}

推荐阅读