我必须运行一个脚本来将特定文件下载到 Internet Explorer 中的所需文件夹,而无需使用 AutoIt 和机器人。除了这个还有别的办法吗。我是自动化新手。 enter image description here enter image description here
最佳答案
我使用了以下代码,它在我这边运行良好,请检查它:
package seleniumtest;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.IOException;
public class testsample {
//@SuppressWarnings("deprecation")
public static void main(String[] args) {
//add the IE web driver path here..
System.setProperty("webdriver.ie.driver","E:\\webdriver\\IEDriverServer_x64_3.14.0\\IEDriverServer.exe");
WebDriver browser = new InternetExplorerDriver();
//replace the URL of the web page here..
browser.get("<website url>");
WebDriverWait wait = new WebDriverWait(browser, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("btnDowloadReport")));
WebElement element = browser.findElement(By.id("btnDowloadReport"));
String downloadurl = element.getAttribute("href");
System.out.println(downloadurl);
String command = "cmd /c E:\\\\Temp\\\\wget.exe -P E:\\\\Temp\\\\output\\\\ --no-check-certificate " + downloadurl;
try {
Process process = Runtime.getRuntime().exec(command);
process.getErrorStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
注意:请记住将网络驱动程序路径、网站网址和下载文件夹更改为您自己的。
网站html资源:
<a id="btnDowloadReport" href="https://github.com//sakinala/AutomationTesting/raw/master/samplefile.pdf" >Download</a>
结果(它将文件下载到所需的文件夹):
关于java - 如何在不使用 autoIt 和机器人类的情况下使用 selenium webdriver 处理 IE 下载弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62085243/