使用Selenium从Chrome商店下载扩展:确认弹窗处理难题
解决Chrome商店下载扩展时的确认弹窗问题
你遇到的这个问题很常见——Chrome的下载确认弹窗不是JavaScript原生的alert对话框,所以driver.SwitchTo().Alert().Accept()根本识别不到它,自然没用。这种属于浏览器级别的系统弹窗,Selenium的WebDriver原生API没法直接处理,不过我们有更简单的办法:直接让Chrome自动跳过确认,默认下载文件。
具体修改方案
你只需要在初始化ChromeOptions的时候,添加下载相关的偏好设置,指定下载目录并设置自动保存,就能彻底绕过这个弹窗:
// 定义下载路径(可以改成你自己的目标路径) string downloadPath = Path.Combine(Environment.CurrentDirectory, "ChromeExtensions"); Directory.CreateDirectory(downloadPath); // 确保目录存在,避免报错 ChromeOptions options = new ChromeOptions(); options.PageLoadStrategy = PageLoadStrategy.None; options.AddArgument("no-sandbox"); options.SetLoggingPreference(LogType.Driver, LogLevel.All); // 核心配置:禁用下载确认弹窗,自动保存到指定目录 var downloadPrefs = new Dictionary<string, object>(); downloadPrefs["download.default_directory"] = downloadPath; downloadPrefs["download.prompt_for_download"] = false; // 关闭下载确认提示 downloadPrefs["download.directory_upgrade"] = true; downloadPrefs["safebrowsing.enabled"] = true; // 信任安全的扩展下载 options.AddUserProfilePreference("download", downloadPrefs); // 可选:如果遇到安全拦截提示,可添加以下参数(仅限测试环境使用) options.AddArgument("--safebrowsing-disable-download-protection"); options.AddArgument("--safebrowsing-disable-extension-blacklist");
修改后的完整代码
把上述配置整合到你的原有代码中,最终版本如下:
using System.IO; using System.Collections.Generic; using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using OpenQA.Selenium.Support.UI; static void WebStoreDownload(string webdriverDirectory) { WebDriverWait wait; // 定义下载路径 string downloadPath = Path.Combine(Environment.CurrentDirectory, "ChromeExtensions"); Directory.CreateDirectory(downloadPath); ChromeOptions options = new ChromeOptions(); options.PageLoadStrategy = PageLoadStrategy.None; options.AddArgument("no-sandbox"); options.SetLoggingPreference(LogType.Driver, LogLevel.All); // 配置自动下载,禁用确认弹窗 var downloadPrefs = new Dictionary<string, object>(); downloadPrefs["download.default_directory"] = downloadPath; downloadPrefs["download.prompt_for_download"] = false; downloadPrefs["download.directory_upgrade"] = true; downloadPrefs["safebrowsing.enabled"] = true; options.AddUserProfilePreference("download", downloadPrefs); // 可选:关闭安全下载保护(测试环境用) options.AddArgument("--safebrowsing-disable-download-protection"); options.AddArgument("--safebrowsing-disable-extension-blacklist"); IWebDriver driver = new ChromeDriver(webdriverDirectory, options, TimeSpan.FromMinutes(1)); driver.Navigate().GoToUrl("https://chrome.google.com/webstore/category/extensions"); wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1)); wait.Until(condition => { try { IWebElement serverTextBox = driver.FindElement(By.Id("searchbox-input")); serverTextBox.Clear(); serverTextBox.SendKeys("malwarebyte"); serverTextBox.SendKeys(Keys.Enter); return true; } catch (NoSuchElementException) { return false; } catch (ElementNotInteractableException) { return false; } }); wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1)); wait.Until(condition => { try { IWebElement toClick = driver.FindElement(By.XPath("//*[contains(text(), 'Malwarebytes Browser Guard')]")); toClick.Click(); return true; } catch (NoSuchElementException) { return false; }catch(ElementNotInteractableException) { return false; } }); Thread.Sleep(3000); wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1)); wait.Until(condition => { try { IWebElement toClick = driver.FindElement(By.ClassName("g-c-R")); toClick.Click(); return true; } catch (NoSuchElementException) { return false; } catch (ElementNotInteractableException) { return false; } }); }
这样修改后,当你点击下载按钮时,Chrome会自动把扩展的.crx文件保存到你指定的目录,完全不会弹出确认窗口。
内容的提问来源于stack exchange,提问作者good_shabes
相关产品推荐
相关产品推荐

