如何使用Selenium上传和下载文件

在 Selenium 中上传文件

在 WebDriver 中上传文件,只需在文件选择输入字段上使用 `sendKeys()` 方法,输入要上传的文件路径即可。

在本教程中,我们将学习如何处理文件上传和下载。

如何在 Selenium 中上传文件

对于本节,我们将使用 https://demo.guru99.com/test/upload/ 作为我们的测试应用程序。该网站允许任何访客轻松上传文件,无需注册。

Upload File in Selenium
处理 Selenium Webdriver 中的文件上传弹窗

假设我们希望上传文件“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();
        }
}

运行此脚本后,您应该能够成功上传文件,并且应该收到类似以下的消息。

Upload File in Selenium

在 WebDriver 中上传文件时,请记住以下两点

  1. 无需模拟点击“浏览”按钮。WebDriver 会自动将文件路径输入到 `` 元素的文件选择文本框中
  2. 在 Java IDE 中设置文件路径时,请使用正确的反斜杠转义字符。

Upload File in Selenium

如何在 Selenium Webdriver 中下载文件

WebDriver 无法访问浏览器在您点击下载链接或按钮时弹出的下载对话框。但是,我们可以使用一个名为“wget”的单独程序绕过这些对话框。

什么是 Wget?

Wget 是一个小型且易于使用的命令行程序,用于自动化下载。基本上,我们将从 WebDriver 脚本中访问 Wget 来执行下载过程。

设置 Wget

步骤 1) 在您的 C 盘中,创建一个新文件夹并将其命名为“Wget”。

此处下载 wget.exe 并将其放置在您在上面步骤中创建的 Wget 文件夹中。

Setting up Wget

步骤 2) 按 Windows 键 + “R” 打开“运行”;输入“cmd”并点击“确定”。

Setting up Wget

输入命令“cd /”以移动到根目录。

Setting up Wget

步骤 3) 输入命令以检查给定设置是否正常工作。

cmd /c C:\\Wget\\wget.exe -P C: --no-check-certificate https://demo.guru99.com/selenium/msgr11us.exe

Setting up Wget

似乎在写入 C 盘时存在问题。

步骤 4) 在使用 Selenium Webdriver 执行代码之前,您需要在命令行中调试 wget 错误。这些错误将持续存在于 Eclipse 中,并且错误消息不会提供太多信息。最好先通过命令行让 wget 工作。如果它在命令行中工作,那么它肯定会在 Eclipse 中工作。

在我们的示例中,如步骤 3 所示,写入 C 盘存在问题。让我们将下载位置更改为 D 盘并检查结果。

cmd /c C:\\Wget\\wget.exe -P D: --no-check-certificate https://demo.guru99.com/selenium/msgr11us.exe

Setting up Wget

Messenger 已成功下载。

在进一步操作之前,请务必删除下载的文件。

使用 WebDriver 和 Wget

在以下示例中,我们将使用 WebDriver 和 wget 下载一款流行的聊天软件,名为 Yahoo Messenger。我们的基本 URL 将是 https://demo.guru99.com/test/yahoo.html

Using WebDriver and Wget

步骤 1) 导入“java.io.IOException”包,因为稍后在步骤 4 中我们将不得不捕获 IOException。

Using WebDriver and Wget

步骤 2) 使用 getAttribute() 获取下载链接的“href”值并将其保存为 String 变量。在此示例中,我们将变量命名为“sourceLocation”。

Using WebDriver and Wget

步骤 3) 使用以下命令设置 wget 语法。

Using WebDriver and Wget

步骤 4) 从我们的 WebDriver 代码中调用 wget,启动下载过程。

Using WebDriver and 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 安装程序是否已成功下载到该处。

Using WebDriver and Wget

摘要

  • 在 WebDriver 中上传文件,只需在文件选择输入字段上使用 `sendKeys()` 方法,输入要上传的文件路径即可。
  • WebDriver 无法自行自动化文件下载。
  • 使用 WebDriver 下载文件最简单的方法是使用 Wget。