selenium下载文件

package com.selenium.gen;

import java.awt.AWTException;

import java.awt.Robot;

import java.io.IOException;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.firefox.FirefoxProfile;

import org.openqa.selenium.interactions.Actions;

import com.thoughtworks.selenium.webdriven.commands.KeyEvent;

public class Download {

public static void main(String[] args) throws AWTException, IOException {

// TODO Auto-generated method stub

FirefoxProfile firefoxprofile=new FirefoxProfile();

//设置保存到指定目录

firefoxprofile.setPreference("browser.download.folderList", 2);

//是否显示开始

firefoxprofile.setPreference("browser.download.manager.showWhenStarting",false);

//指定文件下载目录

firefoxprofile.setPreference("browser.download.dir", "d:\\jase");

//指定要下载页面的 Content-type 值,“application/octet-stream”为文件的类型

firefoxprofile.setPreference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream");

WebDriver driver=new FirefoxDriver(firefoxprofile);

driver.get("https://pypi.Python.org/pypi/selenium");

Actions action=new Actions(driver);

action.click(driver.findElement(By.linkText("selenium-3.0.2.tar.gz"))).perform();

Runtime.getRuntime().exec("C:\\Users\\user\\Desktop\\click.exe");

try {

Thread.sleep(5000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容