【问题标题】:Download a file in IE using Selenium使用 Selenium 在 IE 中下载文件
【发布时间】:2017-06-01 09:24:26
【问题描述】:

好的,所以我正在尝试使用 Selenium 导出文件。我的浏览器是IE。当我单击导出按钮时,会出现本机 Windows 对话框。

弹出窗口的图像

我必须点击保存按钮。为此,我尝试使用AutoIT,但它不起作用。

    exportbutton.click();

    Thread.sleep(2000);

    driver.switchTo().activeElement();

    AutoItX x = new AutoItX();
    x.winActivate("window name");
    x.winWaitActive("window name");

    x.controlClick("window name", "", "[CLASS:Button; INSTANCE:2]");

这不起作用。所以我决定使用 Robot 类并执行键盘点击Atl + S,因为这也将使浏览器能够保存文件。那也没有用。

   try
    {
        Robot robot = new Robot();
         robot.setAutoDelay(250);
         robot.keyPress(KeyEvent.VK_ALT);
         Thread.sleep(1000);
         robot.keyPress(KeyEvent.VK_S);
         robot.keyRelease(KeyEvent.VK_ALT);
         robot.keyRelease(KeyEvent.VK_S);
    }
    catch (AWTException e)
    {
        e.printStackTrace();
    }

我认为网络驱动程序存在一些问题,因为我尝试在exportbutton.click() 之后打印一行,但也没有打印出来。

我是新手,所以我无法理解这个问题。请帮帮我。

【问题讨论】:

    标签: java selenium internet-explorer autoit


    【解决方案1】:

    这是Dave Haefner 的破解。如果您不关心文件是否已下载,并且只想确认可以下载文件,则可以使用 HTTP 请求。您将不会下载文件,而是收到文件的标题信息,其中包含内容类型和长度等内容。有了这些信息,您就可以确认该文件是您所期望的。

        String link = driver.findElement(By.cssSelector("download-link-element")).getAttribute("href");
    
        HttpClient httpClient = HttpClientBuilder.create().build();
        HttpHead request = new HttpHead(link);
        HttpResponse response = httpClient.execute(request);
        String contentType = response.getFirstHeader("Content-Type").getValue();
        int contentLength = Integer.parseInt(response.getFirstHeader("Content-Length").getValue());
    
        assertThat(contentType, is("application/octet-stream"));
        assertThat(contentLength, is(not(0)));
    

    【讨论】:

      【解决方案2】:

      我使用了 AutoIt,它在 windows 10 中运行。请参阅下面的 AutoIt 脚本:

      Sleep(9000);
      Local $hIE = WinGetHandle("[Class:IEFrame]");
      Local $hCtrl = ControlGetHandle($hIE, "", "[ClassNN:DirectUIHWND1]");
      If WinExists($hIE,"") Then
              WinActivate($hIE,"");
              ControlSend($hIE ,"",$hCtrl,"{F6}");
              Sleep(1500);
              ControlSend($hIE ,"",$hCtrl,"{TAB}");
              Sleep(1500);
              ControlSend($hIE ,"",$hCtrl,"{ENTER}");
          EndIf
      Sleep(5000);
      If WinExists($hIE,"") Then
              WinActivate($hIE,"");
              ControlSend($hIE ,"",$hCtrl,"{F6}");
              Sleep(1500);
              ControlSend($hIE ,"",$hCtrl,"{TAB}");
              Sleep(1500);
              ControlSend($hIE ,"",$hCtrl,"{TAB}");
              Sleep(1500);
              ControlSend($hIE ,"",$hCtrl,"{TAB}");
              Sleep(1500);
              ControlSend($hIE ,"",$hCtrl,"{ENTER}");
      EndIf
      Sleep(5000);
      

      它点击保存按钮并关闭下一个警报。

      请相应调整Sleep()

      【讨论】:

        【解决方案3】:

        Auto IT 不需要处理此问题。只需使用下面的代码,它就可以正常工作。 如果我们给 element.click 元素,控制就会停在那里,因此我们使用 element.sendkeys("") 和 robot.keyPress(KeyEvent.VK_ENTER);

        • 下面是完整的代码:

                    Robot robot = new Robot();
          

          //将焦点放在元素上..不要使用点击,因为它会停止驱动程序

                    element.sendKeys("");
           //simulate pressing enter            
                    robot.keyPress(KeyEvent.VK_ENTER);
                    robot.keyRelease(KeyEvent.VK_ENTER);
           //wait for the modal dialog to open            
                    Thread.sleep(2000);
           //press s key to save            
                    robot.keyPress(KeyEvent.VK_ALT);
                    robot.keyPress(KeyEvent.VK_N);
          
                    robot.keyRelease(KeyEvent.VK_N);
                    robot.keyRelease(KeyEvent.VK_ALT);
                    Thread.sleep(2000);
          //press enter to save the file with default name and in default location
                    robot.keyPress(KeyEvent.VK_TAB);
                    robot.keyRelease(KeyEvent.VK_TAB);
          
                    Thread.sleep(2000);
          
                    robot.keyPress(KeyEvent.VK_DOWN);
                    robot.keyRelease(KeyEvent.VK_DOWN);
          
                    Thread.sleep(2000);
          
                    robot.keyPress(KeyEvent.VK_DOWN);
                    robot.keyRelease(KeyEvent.VK_DOWN);
          
                    Thread.sleep(2000);
          
                    robot.keyPress(KeyEvent.VK_ENTER);
                    robot.keyRelease(KeyEvent.VK_ENTER);
          
                    Thread.sleep(2000);
          
                    robot.keyPress(KeyEvent.VK_ENTER);
                    robot.keyRelease(KeyEvent.VK_ENTER);
          

        【讨论】:

          【解决方案4】:

          我遇到了同样的问题。我开始意识到

          button.click()
          

          在这种情况下不能很好地工作(使用 IE 驱动程序)。所以我没有点击按钮,而是尝试了这个:

          robot = new Robot();
          button.sendKeys("""");
          robot.keyPress(KeyEvent.VK_ENTER);
          robot.keyRelease(KeyEvent.VK_ENTER);
          

          这只是将焦点放在按钮上,然后按回车键“按下”它。

          【讨论】:

            【解决方案5】:

            所以,问题是当您调用 click() 函数时,光标有时会卡住。因此,作为解决方案,我使用 Robot 类移动光标并单击导出按钮,然后使用 Robot 类按 Alt+S,这是在 IE 中保存文件的键盘快捷键。

            点击我使用的按钮

            try
            {
                Robot robot = new Robot();
                Thread.sleep(2000);
                robot.mouseMove(coordinates.getX()+100,coordinates.getY()-400); 
                Thread.sleep(2000);
                robot.mousePress( InputEvent.BUTTON1_DOWN_MASK);
                robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
            }
            catch (AWTException e)
            {
                e.printStackTrace();
            }
            

            为了获得上面 sn-p 中的坐标,我使用了以下行

            Point coordinates = driver.findElement(By.id("id")).getLocation();
            System.out.println("Co-ordinates"+coordinates); 
            

            要按 Alt+S,我使用了以下代码

            try
            {
                 Robot robot = new Robot();
                 robot.setAutoDelay(250);
                 robot.keyPress(KeyEvent.VK_ALT);
                 Thread.sleep(1000);
                 robot.keyPress(KeyEvent.VK_S);
                 robot.keyRelease(KeyEvent.VK_ALT);
                 robot.keyRelease(KeyEvent.VK_S);
            }
            catch (AWTException e)
            {
                e.printStackTrace();
            }
            

            【讨论】:

              【解决方案6】:

              对不起,我写了如何上传文件的方法。如果您想下载 - 使用相同的方法,但使用其他按钮:代替按钮 Cntrl + V 您可以使用按钮 Tab 找到保存/另存为的控件,然后按 Enter。在此之前,您可以粘贴带有文件路径的字符串(要上传文件的目录)。

              【讨论】:

              • 这不起作用。我认为我的驱动程序有问题。可能驱动还卡在上一帧?
              • 你基本上是用IE的吗?也许对于当前的测试用例,您应该使用 FireFox 或 Chrome 驱动程序?
              猜你喜欢
              • 1970-01-01
              • 2023-03-21
              • 2021-08-19
              • 2013-11-12
              • 2018-11-10
              • 2020-08-03
              • 2010-11-26
              • 2014-09-27
              相关资源
              最近更新 更多