Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download a file in IE using Selenium

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 enter image description here

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.

like image 249
fatiqnadeem Avatar asked Mar 11 '23 03:03

fatiqnadeem


2 Answers

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();
}
like image 138
fatiqnadeem Avatar answered Mar 12 '23 17:03

fatiqnadeem


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.

like image 35
Vlade Avatar answered Mar 12 '23 15:03

Vlade