zoukankan      html  css  js  c++  java
  • java Selenium 设置文件默认存储路径

    转自:https://blog.csdn.net/zbj18314469395/article/details/81207268

    直接上代码:

    import java.util.HashMap;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.interactions.Actions;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.testng.annotations.AfterClass;
    import org.testng.annotations.Test;
     
    public class testChromeDownload {
        WebDriver driver;
        
        @Test
        public void testOne() throws Exception {            
            //使用Chrome浏览器自动下载文件并保存到指定的文件路径
            //或 使用Selenium更改Chrome默认下载存储路径
            System.setProperty("webdriver.chrome.driver", "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe");//设置驱动的路径    
            DesiredCapabilities caps = setDownloadsPath();//更改默认下载路径        
            driver = new ChromeDriver(caps);
            driver.manage().window().maximize();
            driver.get("https://pypi.org/project/selenium/#files");//到目标网页        
            WebElement myElement = driver.findElement(By.xpath("//a[contains(text(),'selenium-3.13.0.tar.gz')]"));
            Actions action = new Actions(driver);
            myElement.click();//点击下载
            Thread.sleep(10000);
        }
        
        //单独重构成一个方法,然后调用
        public DesiredCapabilities setDownloadsPath() {
            String downloadsPath = "D:\dataSource\outputReport\Downloads";
            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;
        }
        
        @AfterClass
        public void tearDown(){
            driver.quit();
        }
    }
  • 相关阅读:
    [chrome]click事件会触发mouseleave
    鼠标的指针状态 以及 事件禁用
    CSS3 线性渐变(linear-gradient)
    css 的函数 calc() 、linear-gradient()、、、
    1.闰年的计算方法。 2.某一月的周数
    moment.js 使用方法总结
    Echarts 版本查看
    如何使用 onscroll / scrollTo() / scrollBy()
    水平居中、垂直居中
    【LeetCode】22. Generate Parentheses (I thought I know Python...)
  • 原文地址:https://www.cnblogs.com/linwenbin/p/12326718.html
Copyright © 2011-2022 走看看