其他分享
首页 > 其他分享> > WebDriver高级应用实例(3)

WebDriver高级应用实例(3)

作者:互联网

  3.1自动化下载某个文件

  被测网页的网址:

  https://pypi.org/project/selenium/#files

  Java语言版本的API实例代码  

  @Test
  public void testOne() throws Exception {
    //使用Chrome浏览器自动下载文件并保存到指定的文件路径
    //或 使用Selenium更改Chrome默认下载存储路径
    DesiredCapabilities caps = setDownloadsPath();//更改默认下载路径
    driver = new ChromeDriver(caps);
    driver.manage().window().maximize();
    driver.get(url);
    WebElement myElement = driver.findElement(By.xpath("//a[contains(text(),'selenium-3.141.0.tar.gz')]"));
    Actions action = new Actions(driver);
    myElement.click();//点击下载
    Thread.sleep(10000);
  }
  //单独重构成一个方法,然后调用
  public DesiredCapabilities setDownloadsPath() {
    String downloadsPath = "E:\\downloadFiles";
    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;
  }

标签:WebDriver,chromePrefs,driver,高级,caps,ChromeOptions,实例,DesiredCapabilities,new
来源: https://www.cnblogs.com/z-zzz/p/10509066.html