如何使用Selenium WebDriver处理IE下载弹窗,而不使用AutoIt和Robot类。

huangapple go评论96阅读模式
英文:

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>

结果(它将文件下载到所需的文件夹):

如何使用Selenium WebDriver处理IE下载弹窗,而不使用AutoIt和Robot类。

英文:

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(&quot;deprecation&quot;)
	public static void main(String[] args) { 
		
	    //add the IE web driver path here..
        System.setProperty(&quot;webdriver.ie.driver&quot;,&quot;E:\\webdriver\\IEDriverServer_x64_3.14.0\\IEDriverServer.exe&quot;);   

        WebDriver browser = new InternetExplorerDriver();

        //replace the URL of the web page here..
	    browser.get(&quot;&lt;website url&gt;&quot;);
		WebDriverWait wait = new WebDriverWait(browser, 5);
		wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(&quot;btnDowloadReport&quot;)));
		  
		WebElement element = browser.findElement(By.id(&quot;btnDowloadReport&quot;)); 
		String downloadurl = element.getAttribute(&quot;href&quot;);
		System.out.println(downloadurl);
		
		String command = &quot;cmd /c E:\\\\Temp\\\\wget.exe -P E:\\\\Temp\\\\output\\\\ --no-check-certificate &quot; + 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:

 &lt;a id=&quot;btnDowloadReport&quot; href=&quot;https://github.com//sakinala/AutomationTesting/raw/master/samplefile.pdf&quot; &gt;Download&lt;/a&gt;

The result (it will download the file to the desired folder):

如何使用Selenium WebDriver处理IE下载弹窗,而不使用AutoIt和Robot类。

huangapple
  • 本文由 发表于 2020年5月29日 19:46:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/62085243.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定