网上许多修改chrome和firefox浏览器下载路径的方法,都是通过options修改的,对于edge浏览器,selenium中没有对应的方法,只好通过页面修改了。
思路就是:进入下载设置页面–>点击“更改”按钮—>通过autoit3生成控制windows窗口的exe文件—>java的Runtime()类执行exe文件
java代码如下:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class Liulanqi {
public static void main(String[] args) {
System.setProperty("webdriver.edge.driver","D:\\msedgedriver.exe");
WebDriver dr=new EdgeDriver();
//进入edge浏览器的下载设置页面
dr.get("edge://settings/downloads");
dr.manage().window().maximize();
dr.manage().timeouts().implicitlyWait(8, TimeUnit.SECONDS);
//点击更改按钮后会弹出windows的选择位置窗口
dr.findElement(By.xpath("//button[@aria-label=\"更改位置\"]")).click();
//执行使用autoit3调好的exe脚本,该脚本用于操作上一步弹出的windows窗口,输入路径后保存
try {
Runtime.getRuntime().exec("D:\\autoit-v3\\install\\changepath.exe");
} catch (IOException e) {
e.printStackTrace();
}
//使用显示等待,直到路径保存成功了再执行下一步(这一步一定要有!!否则会导致位置修改保存不了)
WebDriverWait wt=new WebDriverWait(dr,30);
wt.until(ExpectedConditions.textToBe(By.xpath("//div[text()='位置']/../p"),"D:\\2012无量寿经讲解音频下载版"));
//打开测试网址开始测试
dr.get("https://www.baidu.com/");
dr.close();
}
}
changepath.exe的生成过程:
1.下载autoit3的包,我的是免安装的.
2.使用包中的Au3info工具作为辅助,写成如下脚本(可以写在txt文档中):
WinWait("位置","",10)
ControlFocus("位置","","[CLASS:Edit; INSTANCE:1]")
Sleep(1000)
ControlSetText("位置","","[CLASS:Edit; INSTANCE:1]","D:\2012无量寿经讲解音频下载版")
Sleep(1000)
ControlFocus("位置","","[CLASS:Button; INSTANCE:1]")
Sleep(1000)
ControlClick("位置","","[CLASS:Button; INSTANCE:1]")
Sleep(4000)
以上就是在位置弹出框中输入路径并保存的意思,该工具的在线文档:https://www.autoitx.com/Doc/
3.使用包中的Aut2Exe工具将txt文件转化为exe,转化好后自己双击执行下,看是否成功。