【问题标题】:How to click and handle dialog window that pop up in IE while downloading file?下载文件时如何点击处理IE中弹出的对话框?
【发布时间】:2018-10-04 11:19:39
【问题描述】:

我想在 java 中使用 selenium webdriver 进行自动化测试。我正在自动下载文件部分,但无法处理在 IE 中下载时弹出的另存为对话窗口。通过单击链接 IE 显示以保存或从弹出窗口打开文件。

 driver.findElement(By.xpath("//img[@alt='145.36KB File Size']")).click();

【问题讨论】:

  • 我认为对话框可能算作“弹出窗口”,这意味着您可能必须切换窗口并“处理”弹出窗口,因为它不是实际网页本身的一部分。任何用于处理弹出窗口的方法都可能是处理此问题的方法。我不认为你可以从 xpath 得到它。此处概述了轻松执行此操作的方法stackoverflow.com/questions/19403949/… 可能是一件好事。

标签: java selenium testng


【解决方案1】:

第一次下载autoit:https://www.autoitscript.com/site/autoit/ 将其添加到 windows 环境变量中的路径,以便可以从命令提示符执行。

示例路径:

C:\Program Files (x86)\AutoIt3

这是一个处理 auto it 脚本的示例方法。

public static void saveFileInternetExplorer() throws Exception {

        String pathToAutoItScript = "C:\\save_file_IE11.au3";
        String command = "AutoIt3.exe " + pathToAutoItScript;
        System.out.println("AutoIt command: " + command );
        String output = new CommandLine(command).executeGetOutput();

        if (output.contains("ERROR")) {
            throw new Exception("AutoIt script error: " + output);
        }

        System.out.println(output);
    }

在 IE 11 中保存文件的 AutoIt 脚本

  • save_file_IE11.au3

Sleep(5000)

    Local $hIE = WinGetHandle("[Class:IEFrame]")
    Local $hCtrl = ControlGetHandle($hIE, "", "[ClassNN:DirectUIHWND1]")

    If WinExists($hIE,"") Then
        WinActivate($hIE,"")
        ControlSend($hIE ,"",$hCtrl,"{F6}")          ; Gives focus to Open Button
        Sleep(500)
        ControlSend($hIE ,"",$hCtrl,"{TAB}")          ; Gives focus to Save Button
        Sleep(500)
        ControlSend($hIE ,"",$hCtrl,"{enter}")        ; Submit whatever control has focus
    EndIf
Sleep(3000)

点击下载按钮后,运行 autoit3 脚本,这应该会保存文件。

祝你好运!

【讨论】:

    猜你喜欢
    • 2016-10-08
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    相关资源
    最近更新 更多