java 使用 Selenium 在 IE 中下载文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41695031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 06:03:56  来源:igfitidea点击:

Download a file in IE using Selenium

javaseleniuminternet-explorerautoit

提问by fatiqnadeem

OK, so I am trying to export a file using Selenium. My browser is IE. When I click on the export button a native windows dialogue box comes up.

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

Image of the pop up enter image description here

弹出窗口的图像 在此处输入图片说明

I have to click on the Save button. For this I tried using AutoITbut its not working.

我必须单击“保存”按钮。为此,我尝试使用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]");

This did not work. So I decided to use Robot class and perform the keyboard clicks Atl + S, as this will also enable the browser to Save the file. That did not work either.

这没有用。所以我决定使用 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();
    }

There is some problem with the web driver I suppose because I tried printing a line after exportbutton.click()and it did not get printed either.

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

I am new so I can't understand the problem. Please help me out.

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

回答by fatiqnadeem

So, the problem was that the cursor gets stuck sometimes when you call the click() function. So as a solution I used the Robot class to move my cursor and click on the export button and then I used Robot class to press Alt+S, which is a keyboard shortcut to save a file in IE.

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

To click on the button I used

单击我使用的按钮

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();
}

To get the coordinates in the above snippet I used the following line

为了获得上面代码片段中的坐标,我使用了以下行

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

And to press Alt+S I used the following code

并按 Alt+SI 使用以下代码

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();
}

回答by Martin ?omodi

I had the same problem. I came to realization that

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

button.click()

does not work very well in this case (with IE driver). So instead of clicking the button I tried this:

在这种情况下(使用 IE 驱动程序)效果不佳。所以我没有点击按钮,而是尝试了这个:

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

This just gives focus on button and 'presses' it by hitting enter.

这只是将焦点放在按钮上并通过按 Enter 键“按下”它。

回答by Devaki Mohan

Auto IT is not required to handle this. just use the below code and it works fine. If we give element.click on the element, control stops there and hence we use element.sendkeys("") and robot.keyPress(KeyEvent.VK_ENTER);

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

  • Below is the complete code:

              Robot robot = new Robot();
    

    //get the focus on the element..don't use click since it stalls the driver

              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);
    
  • 下面是完整的代码:

              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);
    

回答by theBestIsYetToCome

I used AutoIt and it works in windows 10. Refer to the below AutoIt script :

我使用了 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);

It clicks the save button and also closes the next alert.

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

enter image description here

在此处输入图片说明

Please adjust Sleep()accordingly.

请相应调整Sleep()

回答by Mate Mr?e

This is a hack by Dave Haefner. If you don't care if a file was downloaded or not and you want to confirm only that a file can be downloaded, you can use an HTTP request. Instead of downloading the file you'll receive the header information for the file which contains things like the content type and length. With this information, you can confirm the file is you expect.

这是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)));

回答by Schroet

Sorry, I wrote approach how to upload the file. If you want to download - use the same approach, but use another buttons: Instead buttons Cntrl + V you can use button Tab to find control of Save/Save as and then Press Enter. Before it, you can paste String with file path ( directory where you want to upload your file).

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