zoukankan      html  css  js  c++  java
  • 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.*;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.firefox.FirefoxOptions;
    import org.openqa.selenium.firefox.FirefoxProfile;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.Test;
    
    
    /**
     * @description 使用Firefox浏览器下载文件到指定目录演示示例
     * @author rongrong
     * @version 1.0
     * @date 2020/6/27 11:59
     */
    public class TestFirefoxDownload {
        WebDriver driver;
    
        @BeforeClass
        public 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 = new FirefoxOptions();
            //声明一个profile对象
            FirefoxProfile profile = new 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", "application/octet-stream");
            //browser.helperApps.alwaysAsk.force 针对位置的MIME类型文件会弹出窗口让用户处理,默认值为true ,设定为false 表示不会记录打开未知MIME类型文件
            profile.setPreference("browser.helperApps.alwaysAsk.force", true);
            //下载.exe文件弹出窗口警告,默认值是true ,设定为false 则不会弹出警告框
            profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
            //browser.download.manager.focusWhenStarting设定下载框在下载时会获取焦点
            profile.setPreference("browser.download.manager.focusWhenStarting", true);
            //browser.download.manager.useWindow 设定下载是否现在下载框,默认值为true,设定为false 会把下载框隐藏
            profile.setPreference("browser.download.manager.useWindow", false);
            //browser.download.manager.showAlertOnComplete 设定下载文件结束后是否显示下载完成的提示框,默认值为true,
            //设定为false表示下载完成后,现在下载完成提示框
            profile.setPreference("browser.download.manager.showAlertOnComplete", false);
            //browser.download.manager.closeWhenDone 设定下载结束后是否自动关闭下载框,默认值为true 设定为false 表示不关闭下载管理器
            profile.setPreference("browser.download.manager.closeWhenDone", false);
            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 new FirefoxDriver(options);
        }
    
        @Test
        public 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浏览器下载操作

    示例代码如下:

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.interactions.Actions;
    import org.openqa.selenium.remote.CapabilityType;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.testng.annotations.Test;
    
    import java.util.HashMap;
    import java.util.concurrent.TimeUnit;
    
    /**
     * @description 使用Chrome浏览器下载文件到指定目录演示案例
     * @author rongrong
     * @version 1.0
     * @date 2020/6/27 11:59
     */
    public class ChromeDownload {
    
        @Test
        public void testChromeDownload() throws Exception {
            System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe");
            //使用Chrome浏览器自动下载文件并保存到指定的文件路径
            DesiredCapabilities caps = setDownloadsPath();
            WebDriver driver = new ChromeDriver(caps);
            driver.manage().window().maximize();
            //到目标网页,下载搜狗输入法app
            driver.get("https://shouji.sogou.com/download.php");
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            //选择安卓版本下载
            WebElement myElement = driver.findElement(By.cssSelector(".platCont [target='_blank']:nth-of-type(1) span"));
            Actions action = new Actions(driver);
            //点击下载
            myElement.click();
    
        }
    
        /***
         * 设定文件下载目录
         * @return
         */
        public DesiredCapabilities setDownloadsPath() {
            String downloadsPath = "C:\wps";
            HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
            chromePrefs.put("download.default_directory", downloadsPath);
            ChromeOptions options = new ChromeOptions();
            options.setExperimentalOption("prefs", chromePrefs);
            DesiredCapabilities caps = new DesiredCapabilities();
            caps.setCapability(ChromeOptions.CAPABILITY, options);
            return caps;
        }
    }

    测试文件:

    <!DOCTYPE html>
    <html>
    <head>
    
    <title>download</title>
    </head>
    <body>
        <a href="demo.exe">下载</a>
    </body>
    </html>
  • 相关阅读:
    #JavaScript 闭包问题详解 #打倒心魔
    Typora + cnblog 图片自动上传 (超详细哦)
    #FUNCTION#CALL对象中的函数内作用域问题.md
    #windows #Github #HOST
    #######对象迭代器######
    #为什么不建议使用for...in 去遍历数组
    #前后端附件传输,去重的一种方式#解决方案
    #页面滚动刷新的实现原理 #下拉刷新#上拉刷新#drag to fresh
    自己动手实现一个阻塞队列
    APC注入
  • 原文地址:https://www.cnblogs.com/longronglang/p/7417389.html
Copyright © 2011-2022 走看看