如何在不使用autoIt和robots类的情况下用selenium webdriver处理IE下载弹出[已关闭]。

问题描述 投票:0回答:1

我必须在不使用AutoIt和robot的情况下,运行一个脚本将特定的文件下载到Internet Explorer中的所需文件夹。除了这个,还有没有其他的方法。我是自动化方面的新手。请在此输入图片描述请在此输入图片描述

java selenium selenium-webdriver internet-explorer automation
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) { 

        //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>

结果(会把文件下载到所需的文件夹)。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.