在 Selenium 中访问 Firefox 中的文件下载对话框。
我们可以在Selenium的Firefox中访问文件下载对话框。为此,我们必须首先修改存储下载文件的默认目录。这是通过addpreference 方法完成的。
p.addPreference("browser.download.folderList", 2);然后,定义下载目录的新路径。
最后,我们将通过MIME代码忽略文件类型对话框中的保存到磁盘和打开文件选项。addPreference方法可以在FirefoxOptions类的帮助下调用 。
示例
代码实现。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import java.util.concurrent.TimeUnit;
public class FileDwnloadWithoutDialg{
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\geckodriver.exe");
//FirefoxOptions类的实例
FirefoxOptions profile = new FirefoxOptions();
// adding browser preferences with addPreference method profile.addPreference("browser.download.folderList", 2);
//下载文件的位置
profile.addPreference("browser.download.dir", "C:\\Users\\ghs6kor\\Documents\\Download");
profile.addPreference("browser.helperApps.neverAsk.openFile", "text/csv,application/x-msexcel,application/excel," + "application/x-excel,application/vnd.ms-excel," + "image/png,image/jpeg,text/html,text/plain," + "application/msword,application/xml");
profile.addPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv,application/x-msexcel," + "application/excel," + "application/x-excel," +"application/vnd.ms excel,image/png,image/jpeg,text/html," +"text/plain,application/msword,application/xml");
//将浏览器选项连接到webdriver
WebDriver driver = new FirefoxDriver(profile);
driver.get("https://the-internet.herokuapp.com/download");
//最大化窗口
driver.manage().window().maximize();
//识别元素并开始下载
driver.findElement(By.linkText("xls-sample3.xls")).click();
}
}热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下