我正在使用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() {};");
}
GreaseMonkey
的Chrome插件并运行一些脚本,绝对不是理想的选择,或者在您自己的自定义脚本中覆盖onbeforeunload
事件,使用Selenium的JavascriptExecutor(https://superuser.com/questions/593116/how-to-prevent-javascript-alerts-that-fire-when-leaving-a-page) - Ryan Wilson