英文:
How to handle IE download pop up with selenium webdriver without using autoIt and robot class
问题
我必须运行一个脚本,在不使用AutoIt和机器人的情况下,通过Internet Explorer下载特定文件到所需文件夹。除此之外,还有其他方法吗?我对自动化不太熟悉。
英文:
I have to run a script to download a specific file to the desired folder in Internet Explorer without using AutoIt and robot. Is there any other way than this. I am new to automation.
enter image description here
enter image description here
答案1
得分: 0
以下是您提供的代码的翻译部分:
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) {
        // 在此处添加IE Web驱动程序的路径..
        System.setProperty("webdriver.ie.driver", "E:\\webdriver\\IEDriverServer_x64_3.14.0\\IEDriverServer.exe");
        WebDriver browser = new InternetExplorerDriver();
        // 在此处替换网页的URL..
        browser.get("<网站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>
结果(它将文件下载到所需的文件夹):
英文:
I was using the following code, it works well on my side, please check it:
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();
		}       
	}
}
Note: please remember to change the web driver path, website url and the download folder to your owns.
The website html resource:
 <a id="btnDowloadReport" href="https://github.com//sakinala/AutomationTesting/raw/master/samplefile.pdf" >Download</a>
The result (it will download the file to the desired folder):
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论