(十七)文件上传

虫师 创建于 almost 7 years 之前

最后更新: 2 months 之前

阅读数: 1

(十七)文件上传


对于通过input标签实现的上传功能,可以将其看作是一个输入框,即通过sendKeys()指定本地文件路径的方式实现文件上传。

创建upfile.html文件,代码如下:

<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<title>upload_file</title>
<link href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
  <div class="row-fluid">
    <div class="span6 well">
    <h3>upload_file</h3>
      <input type="file" name="file" />
    </div>
  </div>
</body>
<script src="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.js"></scrip>
</html>

通过浏览器打开upfile.html文件,功能如下图。

接下来通过sendKeys()方法来实现文件上传。

import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;


public class UpFileDemo {

  public static void main(String[] args) throws InterruptedException {

    WebDriver driver = new ChromeDriver();
    File file = new File("./HTMLFile/upfile.html");
    String filePath = file.getAbsolutePath();
    driver.get(filePath);

    //定位上传按钮, 添加本地文件
    driver.findElement(By.name("file")).sendKeys("D:\\upload_file.txt");
    Thread.sleep(5000);

    driver.quit();
  }
}
0

相关课程

测试工具合集
图文
测试工具合集

课程分类: 自动化测试 测试工具 中间件

我们收集的各种测试工具

  • 已完结
  • 已更新7集
  • 最后更新时间: 2024-03-18 12:04:41

免费

查看详情
selenium python 综合教程
图文
selenium python 综合教程

课程分类: 自动化测试

selenium python入门及提升

  • 已完结
  • 已更新21集
  • 最后更新时间: 2024-03-18 10:51:39

免费

查看详情
selenium javascript教程
图文
selenium javascript教程

课程分类: 自动化测试

通过selenium来入门js

  • 已完结
  • 已更新21集
  • 最后更新时间: 2024-03-18 10:29:34

免费

查看详情