Downloading file using selenium webdriver Is also tricky task. I have already described how to handle file download functionality In selenium webdriver by creating custom firefox profile In THIS POST. It Is really very long task. At place of using custom profile approach, We can use AutoIt script with selenium webdriver to download file from any web page.
You will find different test files download links on THIS PAGE which we will use to learn how to download file using AutoIt In selenium webdriver. We have to perform bellow given steps to create selenium webdriver + AutoIt script.
In Firefox browser, Go to THIS PAGE and click on "Download Text File" link. It will open Save file dialog with Open with and Save File radio options.
Now you can Identify save file dialog properties using AutoIt Window Info tool as described In THIS POST. In this case you will get only dialog's property (Title and class) using AutoIt Window Info tool as shown In bellow Image. It Is not able to retrieve property of Save File radio option and OK button. So we have to use some alternative method In our AutoIt script to select Save File radio option and click OK button.
So we have only dialog property In this case as bellow.
- File Save dialog : Title = Opening Testing Text.txt, Class = MozillaDialogClass
We will use class property In our script to select dialog.
Write AutoIt script to save file
If you see In above Image, Save File option has shortcut to select It. We can select It using ALT + S keyboard shortcut keys. Also focus Is set by default on OK button. So we can press ENTER key to select OK. We can do both these Keystroke action In AutoIt very easily using Send() command as shown In bellow script.
; wait for 8 seconds to appear download and save dialog. Used class property of download dialog.
WinWait("[CLASS:#MozillaDialogClass]","",8)
; Perform keyboard ALT key down + s + ALT key Up action to select Save File Radio button using keyboard sortcut.
Send("{ALTDOWN}s{ALTUP}")
; Wait for 3 seconds
Sleep(3000)
; Press Keyboard ENTER button.
Send("{ENTER}")
Save above script In "Script To Download File.au3" format at "E:\AutoIT" location. Above script will perform bellow give actions.
- wait for 8 seconds to appear file saving dialog.
- Press ALT down + s + ALT up key to select Save File radio.
- Press Enter key to select OK button.
Convert script In executable format
Now you can create executable file of above script as described In THIS POST. After conversion, you will get executable file "Script To Download File.exe".
CLICK HERE to download ready made "Script To Download File.au3" and "Script To Download File.exe" files of AutoIt.
Now you can create executable file of above script as described In THIS POST. After conversion, you will get executable file "Script To Download File.exe".
CLICK HERE to download ready made "Script To Download File.au3" and "Script To Download File.exe" files of AutoIt.
Integrate AutoIt script with selenium webdriver to download file
As described In THIS POST, We will use java Runtime.getRuntime().exec(); method to execute AutoIt script In selenium webdriver.
So our Selenium webdriver + AutoIt script to download file from web page Is as bellow.
Note : "Script To Download File.exe" file should be located at E:\\AutoIT folder.
package AutoIt;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class AutoIt_Test {
WebDriver driver;
@BeforeTest
public void setup() throws Exception {
driver =new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://only-testing-blog.blogspot.com/2014/05/login.html");
}
@Test
public void testCaseOne_Test_One() throws IOException, InterruptedException {
//Click on Download Text File link to download file.
driver.findElement(By.xpath("//a[contains(.,'Download Text File')]")).click();
//Execute Script To Download File.exe file to run AutoIt script. File location = E:\\AutoIT\\
Runtime.getRuntime().exec("E:\\AutoIT\\Script To Download File.exe");
}
}
Now If you run above script, It will download Text file automatically. At the end of script execution, file will be downloaded as shown In bellow Image.
This way you can download any file from web page.
Good example, Thanks a lot.
ReplyDeleteIs there a way to give the file a name that you want to save it as and in a specific location? Would love to see if we can do that.
ReplyDeleteHi,
DeleteI would like to know answer for this too! Is there a way to give the file a specific name while saving it to our desired location?
I think no.
DeleteHi Aravind,
ReplyDeleteThanks for your informative posts..
How to download and save file in chrome by using AutoIt
Hi, Aravind
ReplyDeleteI follow your post, Go to THIS PAGE and click on "Download Text File" link. It will open Save file dialog with Open with and Save File radio options.My situation is when I click on "Download Text File" link. it download the Text File immediately, there is no dialog to appear.
Thanks. Aravind, it's shocking me, It's not your style. I think maybe
I'm wrong. I will try this section again.
Mak
Hi Aravind,
ReplyDeleteI have downloaded "Script To Download File.exe" files of AutoIt.but it not running.
steps i followed
1.I open the target url in ff browser.
2.click on download button
3.right click on exe file--> run
4.again navigate back to FF browser
5.but radio button selection is not getting changed
Please correct me if anything followed wrong
How to know that file is downloaded or not .
ReplyDeleteThanks