Download Files using Selenium Webdriver

Download Files using Selenium Webdriver

Introduction

WebDriver is a simple, more concise programming interface, in addition to addressing the limitations in the Selenium-RC API. Selenium-WebDriver was developed to support dynamic web pages where elements of a page may change without the page itself being reloaded. WebDriver’s goal is to supply a well-designed object-oriented API that provides improved support for modern advanced web-application testing problems.

Features of Selenium Webdriver:

  • Selenium webdriver supports Java,Python,C#,Ruby,Perl,PHP,Java Script.
  • It is not required to start Selenium server to use Selenium Webdriver.
  • Selenium webdriver supports iPhone and Android testing.

Use Case

Let us download a file from an application in Firefox browser through Selenium Webdriver with a proper settings, time delay and desired path of file, to save.

What we need to do:

  • Open an application in the browser.
  • Find the download button or link in the application.
  • Download the file from the application.
  • Delay the script until the file downloads.
  • Save the downloaded file in a specific location.

Solution

Before solving our case, let’s get some pre-requisites satisfied.

Pre-requisites:

  • Killing ‘defunct’ process
    • Stop running of Firefox instances in the server.
    • If instances are running, use following commands to stop the instances.

To change default Firefox browser settings in a script.

 Challenges faced while downloading a file:

    • Forcing script to wait:

Click the download button in the application. It will get downloaded in a new tab. We have to wait till the application gets downloaded. To make the script wait, use the following code:

  • Tab Switching :

Once the application gets downloaded, we need to close the tab which was opened recently and switch to the parent tab. To navigate to the parent tab, follow the code given below:

  •  .part file process in java:

When we download a big size file under given location using Firefox or when we did not download the file properly, the partially downloaded file will get stored with .part extension under given target location.  In case we start processing the file immediately then, the location contains two type of files (Example: under “root/tool/selenium/”)  #1 .part and #2 .xls or .txt. with the same name

So in this case we have to give a delay time to start and handle/process the stored file, then the .part file will be changed into an actual file and will move out from the downloading location.
Before converting Excel file: Before converting an excel file into a required format, try to open the excel file in EditPlus, if it does not show any encoded character set then it means (showing with delimiter) the file is not an excel file it may be a txt file.

Conclusion

When we are not sure whether the file is available in the destination, we can prefer this type of profile by delaying the settings and then getting the file.

References

14206 Views 6 Views Today