如何使用上传和下载文件 Selenium 网络驱动程序
在 selenium 中上传文件
在 WebDriver 中上传文件只需在文件选择输入字段上使用 sendKeys() 方法输入要上传的文件的路径即可。
在本教程中,我们将学习如何处理文件的上传和下载。
如何上传文件 Selenium
在本节中,我们将使用 https://demo.guru99.com/test/upload/ 作为我们的测试应用程序。此网站允许任何访问者轻松上传文件,而无需他们注册。

假设我们要上传文件“C:\newhtml.html”。我们的 WebDriver 代码应如下所示。
package newproject; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; public class PG9 { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe"); String baseUrl = "https://demo.guru99.com/test/upload/"; WebDriver driver = new FirefoxDriver(); driver.get(baseUrl); WebElement uploadElement = driver.findElement(By.id("uploadfile_0")); // enter the file path onto the file-selection input field uploadElement.sendKeys("C:\\newhtml.html"); // check the "I accept the terms of service" check box driver.findElement(By.id("terms")).click(); // click the "UploadFile" button driver.findElement(By.name("send")).click(); } }
运行此脚本后,您应该能够成功上传文件,并且应该收到类似这样的消息。
在 WebDriver 中上传文件时请记住以下两点
- 无需模拟单击“浏览”按钮。WebDriver 会自动将文件路径输入到元素
- 在设置文件路径时 Java IDE,使用正确的反斜杠转义字符。
如何下载文件 Selenium 网络驱动程序
WebDriver 无法访问下载对话框 单击下载链接或按钮时,浏览器会弹出下载对话框。但是,我们可以使用名为“wget”的单独程序绕过这些对话框。
什么是 Wget?
Wget 是一个小型且易于使用的命令行程序,用于自动下载. 基本上,我们将从我们的 WebDriver 脚本访问 Wget 来执行下载过程。
设置 Wget
步骤1) 在 C 盘中,创建一个新文件夹并将其命名为“Wget”。
下载 wget.exe 从这里开始 并将其放在您在上述步骤中创建的 Wget 文件夹中。
步骤2) 按 Windows 键 + “R” 打开运行;输入“cmd 并单击确定
输入命令“cd /”移动到根目录
步骤3) 输入命令来检查给定的设置是否正常工作
cmd /c C:\\Wget\\wget.exe -P C: --no-check-certificate https://demo.guru99.com/selenium/msgr11us.exe
写入 C 盘似乎存在问题。
步骤4) 您需要在命令行中调试 wget 错误,然后才能使用以下命令执行代码 Selenium Webdriver。这些错误将持续存在 Eclipse 错误消息不会那么有用。最好先使用命令行让 wget 工作。如果它在命令行中工作,那么它肯定会在 Eclipse.
在我们的示例中,如步骤 3 所示,写入 C 盘时出现问题。让我们将下载位置更改为 D 盘并检查结果。
cmd /c C:\\Wget\\wget.exe -P D: --no-check-certificate https://demo.guru99.com/selenium/msgr11us.exe
Messenger 已成功下载。
在继续操作之前,请不要忘记删除下载的文件
使用 WebDriver 和 Wget
在以下示例中,我们将使用 WebDriver 和 wget 下载名为 Yahoo 的流行聊天软件 Messenger. 我们的基础 URL 应为 https://demo.guru99.com/test/yahoo.html.
步骤1) 导入“java.io.IOException”包,因为我们必须在后面的第 4 步中捕获 IOException。
步骤2) 使用 getAttribute() 获取下载链接的“href”值并将其保存为 String 变量。在本例中,我们将该变量命名为“sourceLocation”。
步骤3) 使用以下命令设置 wget 的语法。
步骤4) 通过从我们的 WebDriver 代码调用 wget 来启动下载过程。
总而言之,您的 WebDriver 代码可能看起来像下面显示的那样。
package newproject; import java.io.IOException; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; public class PG8 { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver","C:\\geckodriver.exe"); String baseUrl = "https://demo.guru99.com/test/yahoo.html"; WebDriver driver = new FirefoxDriver(); driver.get(baseUrl); WebElement downloadButton = driver.findElement(By .id("messenger-download")); String sourceLocation = downloadButton.getAttribute("href"); String wget_command = "cmd /c C:\\Wget\\wget.exe -P D: --no-check-certificate " + sourceLocation; try { Process exec = Runtime.getRuntime().exec(wget_command); int exitVal = exec.waitFor(); System.out.println("Exit value: " + exitVal); } catch (InterruptedException | IOException ex) { System.out.println(ex.toString()); } driver.close(); } }
执行此代码后,检查您的 D 盘并验证 Yahoo Messenger 安装程序已成功下载。
总结
- 在 WebDriver 中上传文件只需在文件选择输入字段上使用 sendKeys() 方法输入要上传的文件的路径即可。
- WebDriver 无法自动下载文件。
- 使用 WebDriver 下载文件最简单的方法是使用 Wget。