Selenium + java不借助autolt实现下载文件到指定目录

前端之家收集整理的这篇文章主要介绍了Selenium + java不借助autolt实现下载文件到指定目录前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

今天尝试了下不用借助autolt完成下载文件到指定目录。

好处:在于集成回归,远程机可以绕过执行autolt程序权限问题,导致autolt程序无法调用,不能完成脚本的回归。

Firefox浏览器下载操作

对应版本:

Firefox 版本是77.0.1
geckodriver 是 0.260
selenium 是3.9.1

代码中已有注释,具体看查看代码就好

示例代码如下:

import java.io.File;

import org.openqa.selenium.*;
 org.openqa.selenium.firefox.FirefoxDriver;
 org.openqa.selenium.firefox.FirefoxOptions;
 org.openqa.selenium.firefox.FirefoxProfile;
 org.testng.annotations.BeforeClass;
 org.testng.annotations.Test;


/**
 * @description 使用Firefox浏览器下载文件到指定目录演示示例
 * @author rongrong
 * @version 1.0
 * @date 2020/6/27 11:59
 */
public class TestFirefoxDownload {
    WebDriver driver;

    @BeforeClass
    void beforeClass() {
        driver = getDriver();
    }

    
     * 设置火狐浏览器默认参数
     *
     * @return
     */
    private WebDriver getDriver() {
        //FirefoxProfile profile = new FirefoxProfile();
         可以在Firefox浏览器地址栏中输入about:config来查看属性
         设置下载文件放置路径,注意如果是windows环境一定要用\\,用/不行
        String path = "C:\\wps";
        String downloadFilePath = path + "\\demo.exe";
        File file = new File(downloadFilePath);
        if (file.exists()) {
            file.delete();
        }
        FirefoxOptions options =  FirefoxOptions();
        声明一个profile对象
        FirefoxProfile profile =  FirefoxProfile();
        设置Firefox的“broswer.download.folderList”属性为2
        
         * 如果没有进行设定,则使用默认值 1,表示下载文件保存在“下载”文件夹中
         * 设定为0,则下载文件会被保存在用户的桌面上
         * 设定为2,则下载的文件会被保存的用户指定的文件夹中
         */
        profile.setPreference("browser.download.folderList",2);
        browser.download.manager.showWhenStarting的属性默认值为true
        设定为 true , 则在用户启动下载时显示Firefox浏览器的文件下载窗口
        设定为false,则在用户启动下载时不显示Firefox浏览器的文件下载窗口
        profile.setPreference("browser.download.manager.showWhenStarting",false设定文件保存的目录
        profile.setPreference("browser.download.dir",path);
        browser.helperApps.neverAsk.openFile 表示直接打开下载文件不显示确认框
        默认值.exe类型的文件,"application/excel"表示Excel类型的文件
            application/x-msdownload
        profile.setPreference("browser.helperApps.neverAsk.openFile","application/octet-stream"browser.helperApps.never.saveToDisk 设置是否直接保存 下载文件到磁盘中默认值为空字符串,厦航代码行设定了多种温江的MIME类型
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk",1)">browser.helperApps.alwaysAsk.force 针对位置的MIME类型文件会弹出窗口让用户处理,默认值为true ,设定为false 表示不会记录打开未知MIME类型文件
        profile.setPreference("browser.helperApps.alwaysAsk.force",1)">true下载.exe文件弹出窗口警告,默认值是true ,设定为false 则不会弹出警告框
        profile.setPreference("browser.download.manager.alertOnEXEOpen",1)">browser.download.manager.focusWhenStarting设定下载框在下载时会获取焦点
        profile.setPreference("browser.download.manager.focusWhenStarting",1)">browser.download.manager.useWindow 设定下载是否现在下载框,默认值为true,设定为false 会把下载框隐藏
        profile.setPreference("browser.download.manager.useWindow",1)">browser.download.manager.showAlertOnComplete 设定下载文件结束后是否显示下载完成的提示框,默认值为true,
        设定为false表示下载完成后,现在下载完成提示
        profile.setPreference("browser.download.manager.showAlertOnComplete",1)">browser.download.manager.closeWhenDone 设定下载结束后是否自动关闭下载框,默认值为true 设定为false 表示不关闭下载管理器
        profile.setPreference("browser.download.manager.closeWhenDone",1)">);
        options.setProfile(profile);
        设置系统变量,并设置 geckodriver 的路径为系统属性
        System.setProperty("webdriver.gecko.driver","driver/geckodriver.exe"导入Firefox浏览器安装路径
        System.setProperty("webdriver.firefox.bin","E:/Program Files/Mozilla Firefox/firefox.exe"return  FirefoxDriver(options);
    }

    @Test
    void test() throws InterruptedException {
        driver.get("http://localhost:8080/download.html");
        driver.manage().window().maximize();
        driver.findElement(By.linkText("下载")).click();
        Thread.sleep(3000);
    }
}

 查看下载文件内容类型

也就是下载文件的Content-type是什么,具体查看步骤如下图所示:

 

 

 

 

Chrome浏览器下载操作

示例代码如下:

 org.openqa.selenium.By;
 org.openqa.selenium.WebElement;
 org.openqa.selenium.chrome.ChromeDriver;
 org.openqa.selenium.chrome.ChromeOptions;
 org.openqa.selenium.WebDriver;
 org.openqa.selenium.interactions.Actions;
 org.openqa.selenium.remote.CapabilityType;
 org.openqa.selenium.remote.DesiredCapabilities;
 org.testng.annotations.Test;

 java.util.HashMap;
 java.util.concurrent.TimeUnit;


 * @description 使用Chrome浏览器下载文件到指定目录演示案例
 *  ChromeDownload {

    @Test
    void testChromeDownload()  Exception {
        System.setProperty("webdriver.chrome.driver","driver/chromedriver.exe"使用Chrome浏览器自动下载文件并保存到指定的文件路径
        DesiredCapabilities caps = setDownloadsPath();
        WebDriver driver =  ChromeDriver(caps);
        driver.manage().window().maximize();
        到目标网页,下载搜狗输入法app
        driver.get("https://shouji.sogou.com/download.PHP");
        driver.manage().timeouts().implicitlyWait(30选择安卓版本下载
        WebElement myElement = driver.findElement(By.cssSelector(".platCont [target='_blank']:nth-of-type(1) span"));
        Actions action =  Actions(driver);
        点击下载
        myElement.click();

    }

    *
     * 设定文件下载目录
     * public DesiredCapabilities setDownloadsPath() {
        String downloadsPath = "C:\\wps";
        HashMap<String,Object> chromePrefs = new HashMap<String,Object>();
        chromePrefs.put("download.default_directory" ChromeOptions();
        options.setExperimentalOption("prefs" DesiredCapabilities();
        caps.setCapability(ChromeOptions.CAPABILITY,options);
        return caps;
    }
}

 

测试文件

<!DOCTYPE html>
<htmlhead>

title>download</body>
    a href="demo.exe">下载a>

 

猜你在找的Selenium相关文章