首页 > 解决方案 > 如何在 Selenium 中设置 Edge 的下载文件夹?

问题描述

我正在使用 Selenium 编写自动化测试。我想在 Edge 中设置下载目录,以便我可以下载文件作为测试的一部分。我可以在创建 EdgeDriver 时提供一个 EdgeOptions 对象,但我不知道在 EdgeOptions 上设置什么。

我知道相当于如何在 Chrome 中执行此操作

chromeOptions.AddUserProfilePreference("download.default_directory", @"C:\temp")

和火狐

firefoxOptions.SetPreference("browser.download.dir", @"C:\temp")

但是,我如何在 Edge 中做同样的事情?并让它在没有保存提示的情况下自动下载?

标签: c#seleniumselenium-webdriverselenium-edgedriver

解决方案


正如@Prany 已经提到的,可能无法自动设置下载。如果我理解正确,当您单击下载按钮时,您想处理本机窗口对话。Selenium 不能与原生窗口交互,但你可以使用这个框架。示例代码如下所示:

// Press the A Key Down
KeyboardSimulator.KeyDown(Keys.A);

// Let the A Key back up
KeyboardSimulator.KeyUp(Keys.A);

// Press A down, and let up (same as two above)
KeyboardSimulator.KeyPress(Keys.A);

// Simulate (Ctrl + C) shortcut, which is copy for most applications
KeyboardSimulator.SimulateStandardShortcut(StandardShortcut.Copy);

// This does the same as above
KeyboardSimulator.KeyDown(Keys.Control);
KeyboardSimulator.KeyPress(Keys.C);
KeyboardSimulator.KeyUp(Keys.Control);

所以可以模拟Ctrl + V键盘动作和Enter动作。希望能帮助到你。


推荐阅读