Selenium禁用恢复页面弹窗。

6

我正在使用 Selenium C#,尝试禁用“崩溃”Chrome的弹出窗口:

https://istack.dev59.com/xudon.webp

我尝试设置配置文件偏好设置,但似乎根本没有改变,代码如下:

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

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

6个回答

2

我测试了@Icy提供的答案,对我有效。我使用的是:

prefs = {'exit_type': 'Normal'}
option.add_experimental_option("prefs", {'profile': prefs})

这个问题在https://superuser.com/a/1343331中有提到,但是那里列出的方法需要每次手动编辑文件,所以这种方法更好,经过2021年5月测试。由于我还没有声望,所以无法点赞该回答,而且这也是最后一个回答。


2

我正在使用C#,注意到只有在finally块中使用Close()方法后跟Quit()方法才能正确关闭chrome driver。不需要任何特殊选项。我认为在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) 
    {
    }
}

1

经过一个月的搜索,我找到了一个解决方案。 无论在哪里都写着的--disable-session-crashed-bubble参数并不相关。使用--hide-crash-restore-bubble。通过使用 Python + Selenium 进行测试,有效。

示例:

...
    def _options(self):
        chrome_options = Options()
        chrome_options.add_argument('--hide-crash-restore-bubble')
        # some other options
        return chrome_options
...

0
请使用以下代码来处理此弹出窗口:
ChromeOptions options = new ChromeOptions();
options.AddArguments("--disable-extensions");
options.AddArguments("--disable-application-cache");
driver = new ChromeDriver(options);

如果我不想禁用整个浏览器缓存怎么办? - delex
只需删除 options.AddArguments("--disable-application-cache");。基本上针对您的问题,options.AddArguments("--disable-extensions"); 这个选项就足够了。 - Raj
2
很遗憾,这并没有解决我的问题,而且我已经尝试使用了这两个命令。 - delex
你可以尝试使用这个开关 "--disable-infobars" 以及 "--disable-session-crashed-bubble" 吗? - Raj
仍然没有任何改变。 - delex

0

我在Java中尝试了这段代码,它解决了我的问题:))

    ChromeOptions options = new ChromeOptions();
    options.addArguments("user-data-dir="+profilepath);     
    options.addArguments("--no-startup-window");
    // argument "--no-startup-window" make chrome is failed to start -> selenium will quit chrome normaly 
    //-> start chrome again, it won't show restore page
    try {
        driver = new ChromeDriver(options); 
    }catch(Exception ex){                           
    }
    options = new ChromeOptions();
    options.addArguments("user-data-dir="+profilepath);
    driver = new ChromeDriver(options);

-1

尝试这段代码:

prefs = {'exit_type': 'Normal'}

chrome_options.add_experimental_option("prefs", {'profile': prefs})

一些解释说明为什么这有助于回答OP的问题,将帮助未来寻求解决类似问题的人。 - SiKing

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接