如何禁用“您确定要离开此页面”的警告?

3

我正在使用c# selenium webdriver。我想关闭chrome的警告提示。例如,禁用“确定要离开此页面”的提示框。以下代码均无效。您可以帮助提供解释性代码。

private void Form1_Load(object sender, EventArgs e)
{
    ChromeOptions options = new ChromeOptions();
    options.AddArgument("--disable-notifications");
    options.AddArguments("--disable-extensions"); // to disable extension
    options.AddArguments("--disable-notifications"); // to disable notification
    options.AddArguments("--disable-application-cache"); // to disable cache
    options.AddArgument("--safebrowsing-disable-download-protection");
    options.AddArgument("ignore-certificate-errors");
    options.AddArgument("--disable-popup-blocking");
    options.AddArgument("--disable-gpu");
    options.AddArgument("--incognito");
    options.AddUserProfilePreference("disable-popup-blocking", "true");
    driveri = new ChromeDriver(options); 
    driveri.Navigate().GoToUrl("http://web.com");

    IJavaScriptExecutor js = (IJavaScriptExecutor)driveri; 
    js.ExecuteScript("window.alert = function() {};");
}

1
没有Chrome设置可以停止这个,唯一的选择是添加一个名为GreaseMonkey的Chrome插件并运行一些脚本,绝对不是理想的选择,或者在您自己的自定义脚本中覆盖onbeforeunload事件,使用Selenium的JavascriptExecutor(https://superuser.com/questions/593116/how-to-prevent-javascript-alerts-that-fire-when-leaving-a-page) - Ryan Wilson
1
请查看此讨论 https://stackoverflow.com/questions/51555208/how-to-handle-browser-notification-popup-which-is-without-any-elements - undetected Selenium
1个回答

3

嗨,我通过名为“Let Me Out”的Chrome扩展程序解决了我的问题。也许这正是他所需要的。以下是我的示例代码:

 ChromeOptions options = new ChromeOptions();
 options.AddArgument("--disable-notifications");
 options.AddExtensions(@"C:\Let-Me-Out_v1.3.crx");
 driveri = new ChromeDriver(options); 

@"C:\Let-Me-Out_v1.3.crx" 我需要把它放在路径中吗?相应的Python代码是什么? - Marx Babu
这似乎不是一个干净的解决方案,你应该解释一下为什么要放C:Let-me_Out_V1.3.crx.... 有没有Python的干净解决方案? - Marx Babu

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