iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Java+Selenium实现文件上传下载功能详解
  • 729
分享到

Java+Selenium实现文件上传下载功能详解

Python 官方文档:入门教程 => 点击学习

摘要

目录简介上传文件下载文件简介 本文主要讲解java代码如何利用selenium操作浏览器上传和下载文件代码教程。 上传文件 常见的 WEB 页面的上传,一般使用 input 标签或是

简介

本文主要讲解java代码如何利用selenium操作浏览器上传和下载文件代码教程

上传文件

常见的 WEB 页面的上传,一般使用 input 标签或是插件javascriptajax),对于 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");

到此这篇关于Java+Selenium实现文件上传下载功能详解的文章就介绍到这了,更多相关Java Selenium文件上传下载内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Java+Selenium实现文件上传下载功能详解

本文链接: https://www.lsjlt.com/news/177139.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作