Download a file in IE using Selenium

廉价感情. 提交于 2019-12-30 05:09:25

问题


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.

Image of the pop up

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

    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.

   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.

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


回答1:


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.

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

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



回答2:


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:

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.




回答3:


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




回答4:


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

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



回答5:


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

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.

Please adjust Sleep() accordingly.




回答6:


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.

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


来源:https://stackoverflow.com/questions/41695031/download-a-file-in-ie-using-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!