【问题标题】:How to handle IE download pop up with selenium webdriver without using autoIt and robot class [closed]如何在不使用 autoIt 和机器人类的情况下使用 selenium webdriver 处理 IE 下载弹出 [关闭]
【发布时间】:2020-09-16 23:32:27
【问题描述】:

我必须运行一个脚本来将特定文件下载到 Internet Explorer 中所需的文件夹,而不使用 AutoIt 和机器人。除了这个还有其他办法吗。我是自动化新手。 enter image description here enter image description here

【问题讨论】:

  • 据我所知,当您单击下载链接或按钮时,WebDriver 无法访问浏览器显示的 IE 下载对话框。但是,我们可以使用名为“wget”的单独程序绕过这些对话框。更多详情,您可以查看this article
  • 这个我已经试过了。但没有运气......仍然有一些错误
  • 您使用哪种语言进行 Web 驱动程序测试,Java?可以看到,如果使用 wget 方法,我们应该先获取超链接的 href 属性,然后根据 url 下载文件。您能否分享有关如何在您的网页中下载文件的相关代码。另外,我找到了一个关于使用机器人类下载文件的示例,在我这方面效果很好,您可以考虑使用这种方法吗?
  • 包 Download_file.Download_file;导入 org.openqa.selenium.By;导入 org.openqa.selenium.WebDriver;导入 org.openqa.selenium.WebElement;导入 org.openqa.selenium.ie.InternetExplorerDriver; public class file_IE_mini { //文件已下载,但等待一段时间后未打印退出 val public static void main(String[] args) throws Exception { // TODO 自动生成的方法 stub System.setProperty("webdriver.ie.driver" , "IEDriverServer.exe"); WebDriver dr= new InternetExplorerDriver(); dr.get("demo.automationtesting.in/FileDownload.html");
  • System.out.println(dr.getTitle()); // 使用getAttribute()获取下载链接的“href”值,保存为String变量。在这种情况下,我们将变量命名为“sourceLocation”。 WebElement downloadButton = dr.findElement(By.xpath("//a[@class='btn btn-primary']")); String sourceLocation = downloadButton.getAttribute("href");

标签: java selenium selenium-webdriver internet-explorer automation


【解决方案1】:

我正在使用以下代码,它在我这边运行良好,请检查:

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>

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

【讨论】:

  • 它运行得非常好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-21
  • 1970-01-01
  • 2017-01-02
  • 1970-01-01
  • 2012-04-23
相关资源
最近更新 更多