Java+Selenium实现文件上传下载功能详解
作者:洛阳泰山 时间:2021-06-23 23:33:23
简介
本文主要讲解java代码如何利用selenium操作浏览器上传和下载文件代码教程。
上传文件
常见的 web 页面的上传,一般使用 input 标签或是插件(JavaScript、Ajax),对于 input 标签的上传,可以直接使用 sendKeys(路径) 来进行上传。
先写一个测试用的页面。
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<input type="file" name="">
</body>
</html>
下面通过 xpath 定位 input 标签,然后使用 sendKeys(filePath) 上传文件。
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.awt.*;
import java.io.IOException;
public class SeleniumDemo {
private final static String webDriver = "webdriver.chrome.driver";
private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe";
public static void main(String[] args) throws InterruptedException, IOException, AWTException {
System.setProperty(webDriver, webDriverPath);
WebDriver driver= new ChromeDriver();
driver.get("file:///C:/Users/liuya/Desktop/test.html");
Thread.sleep(2000);
String filePath="C:\\Users\\liuya\\Desktop\\doc\\tarzan.txt";
driver.findElement(By.xpath("//*[@name='upload']")).sendKeys(filePath);
}
}
下载文件
Chrome浏览器
Firefox 浏览器要想实现文件下载,需要通过 add_experimental_option 添加 prefs 参数。
download.default_directory:设置下载路径。
profile.default_content_settings.popups:0 禁止弹出窗口。
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.awt.*;
import java.io.IOException;
public class SeleniumDemo {
private final static String webDriver = "webdriver.chrome.driver";
private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe";
public static void main(String[] args) throws InterruptedException, IOException, AWTException {
System.setProperty(webDriver, webDriverPath);
WebDriver driver= new ChromeDriver();
driver.get("http://pic.sogou.com/d?query=%E5%B0%8F%E7%8B%97&forbidqc=&entityid=&preQuery=&rawQuery=&queryList=&st=&did=45");
Thread.sleep(2000);
driver.findElement(By.className("download")).click();
}
}
当你弹出像下面的页面 “您的连接不是私密连接” 时,可以直接键盘输入 “thisisunsafe” 直接访问链接。那么这个键盘输入字符串的操作就是之间讲到的 sendKeys,但由于该标签页是新打开的,所以要通过 switchTo().window() 将窗口切换到最新的标签页。
//操作最新窗口
driver.switchTo().window(driver.getWindowHandles().stream().reduce((first, second) -> second).orElse(null));
driver.findElement(By.xpath("./html")).sendKeys("thisisunsafe");
来源:https://tarzan.blog.csdn.net/article/details/128601702
标签:Java,Selenium,文件,上传,下载
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot自动装配之Condition深入讲解
2023-12-03 02:20:29
Java编程实现对十六进制字符串异或运算代码示例
2023-11-06 15:58:15
![](https://img.aspxhome.com/file/2023/2/58892_0s.png)
Java 入门图形用户界面设计之单选按钮
2023-08-29 13:40:02
![](https://img.aspxhome.com/file/2023/2/108362_0s.png)
java 可变参数详解及实例
2021-06-01 10:02:42
C#中DataGridView操作技巧
2023-05-03 20:37:30
Java桶排序之基数排序详解
2021-08-26 13:38:58
![](https://img.aspxhome.com/file/2023/7/118907_0s.jpg)
Spring-Bean创建对象的步骤方式详解
2023-04-12 22:00:47
Windows下安装ElasticSearch的方法(图文)
2023-11-25 13:35:35
![](https://img.aspxhome.com/file/2023/2/60192_0s.png)
Android webview与js的数据交互
2021-08-18 02:56:32
![](https://img.aspxhome.com/file/2023/7/109197_0s.jpg)
详解Spring与Mybatis整合方法(基于IDEA中的Maven整合)
2023-11-06 21:35:12
![](https://img.aspxhome.com/file/2023/0/58690_0s.png)
java求最大公约数与最小公倍数的方法示例
2023-01-23 03:28:58
![](https://img.aspxhome.com/file/2023/6/87366_0s.png)
C++中静态成员函数与静态成员变量(static )
2021-11-08 07:31:38
Android ListView和Adapter数据适配器的简单介绍
2023-04-16 04:07:12
![](https://img.aspxhome.com/file/2023/4/97854_0s.png)
SpringCloud 客户端Ribbon负载均衡的实现方法
2023-03-22 16:42:43
![](https://img.aspxhome.com/file/2023/6/118886_0s.png)
mybatis多表查询的实现(xml方式)
2021-07-11 01:21:38
![](https://img.aspxhome.com/file/2023/5/79235_0s.png)
Java 反转带头结点的单链表并显示输出的实现过程
2022-06-08 18:34:39
![](https://img.aspxhome.com/file/2023/0/110610_0s.jpg)
Java concurrency之公平锁(二)_动力节点Java学院整理
2023-11-24 21:14:54
解决mybatis三表连接查询数据重复的问题
2023-09-13 15:24:25
![](https://img.aspxhome.com/file/2023/2/105912_0s.jpg)
浅谈C#中List<T>对象的深度拷贝问题
2022-12-16 13:09:10
spring boot 使用profile来分区配置的操作
2022-11-27 22:55:15
![](https://img.aspxhome.com/file/2023/5/64205_0s.png)