How to download a File using Python and Selenium

How to download a File using Python and Selenium

This article provides you with a solution for downloading a file using python and selenium in a folder. Handling files can be a tedious task at times. Especially, when you have test scenarios like downloading a file and verifying if the file is downloaded and if yes then delete the downloaded file. 

Despite visiting many websites and reading many articles, I was not able to find the right solution. Here, I am providing all the solutions in one place, as visiting multiple web pages to find a single solution is tiring. Here, we are using the python and selenium combination to download a file in a folder. You can use the language you like for example, java, javascript, c#, etc. After reading this article, you will get to know how you can handle this type of scenario and we will solve this issue together. So just follow the steps described. 

Traditional Approach:

When you download any file from the website it generally gets downloaded in your download folder i.e. on your local system, but here, is what we are doing we are creating a folder download in our framework. Then we download that file in this newly created folder. 

Till this point, I assume you have understood the test scenario and also we will be passing the file name to delete the particular file. Also, to verify whether the particular file is getting downloaded or not.

It would help if you imported some packages of python and selenium they have listed below.  

To change the download folder path from our local system to the framework folder we need to add some script here, that will set the new download folder as our default folder, to download the files from the webpage. 

Step1:

Import the following packages.

from selenium import webdriver
import os
From selenium.webdriver.common.by import By
From webdriver_manager.chrome import ChromeDriverManager

After adding the above imports now we will have to change the path to do so see the script and you will get an idea. 

op = webdriver.ChromeOptions()
op.add_argument('--no-sandbox')
op.add_argument('--verbose')
op.add_argument("--disable-notifications")
op.add_experimental_option("prefs", {
  "download.default_directory": "G:/Python/Download/",
  "download.prompt_for_download": False,
  "download.directory_upgrade": True,
  "safebrowsing.enabled": True})
op.add_argument('--disable-gpu')
op.add_argument('--disable-software-rasterizer')

Now we have set the download path to our new folder now we have to set the driver. 

Step2:

Here, I have used the web driver manager you can use the chrome driver and provide the path if you want to. But, I suggest using web driver manager, as it is a good practice to use. Because it will download all the updated chrome driver versions automatically and you will save lots of your time. 

driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=op)

I hope no one has any problems or doubts till this point, as these steps are crucial and if you have any doubts go through the steps again.  Now you can launch your webpage URL. 

Step3:

Here, I want you to write your script to locate the web element and click on that element for example refer to the following scrip

driver.find_element(By.XPATH, “//span[@type = ‘button’]”).click()

After clicking, the file will get downloaded in the new download folder that we have created in our framework. 

Step4:

The next step is to see if the file is present in the newly created download folder. In order to achieve this just go through the following script. 

def download_file_verify(self,filename):
       dir_path = "G:/Python/Download/"
        res = os.listdir(dir_path)
        try:
            name = os.path.isfile("Download/" + res[0])
            if res[0].__contains__(filename):
                print("file downloaded successfully")
        except "file is not downloaded":
            name = False
        return name

Here, you can provide the name of your downloaded file to the filename argument to avoid the hard coding of the script, as it is not a good practice. 

Explanation:

For instance, the name of my downloaded file is extent report so now the value of the filename argument becomes extent report.

So, first of all, it will go to the directory path we have provided now it will store all the file names already present in the folder in a list format. 

Here we have stored that list in the res variable. Now we can iterate over the list and verify if our desired file is present in the folder or not. 

Take note here, that the newly downloaded file will always be present in the zeroth index of our download folder. That is why we have used res[0] to check, if the downloaded file is present at the zeroth index or not. 

Now, it will check if the zeroth index file name is equal to that of the name of the file we have provided. So, if yes then it will print(“file downloaded successfully”), and if not then it will throw an exception and will print(“file is not downloaded”)

Here I have used assertion to verify whether the file is downloaded or not. I will suggest you use the same as it is good practice. You will get to know the assertion while handling the file. 

Congratulations, we are done with the first part. We have successfully downloaded the file in the newly created download folder. We have also verified whether the file is downloaded or not. 

Step5:

The next task is to delete the downloaded file by passing the name of the file. So, let’s get started then. 

Script to delete the file from the download folder by passing the name of the file. 

def delete_previous_file(self,filename):
        try:
            d_path = "G:/Python/Download/"
            list = os.listdir(d_path)
            for file in list:
                print("present file is: " + file)
                path = ("Download/" + file)
                if file.__contains__(filename):
                    os.remove(path)
                    print("Present file is deleted")
        except:
            pass

Explanation:

Here, we don’t have to only delete the file that is present at the zeroth index. But we have to delete all the files present in the download folder with the same file name. So, that when a new file gets downloaded there will be only one file present. 

So the above code will first go to the directory path. Store all the file names present as a list. So, now we have to iterate over that list and see if the same file is present. If yes then we have to delete that file. 

Use try and except block. Here, if there is no file present, then our code will not raise any exceptions or will fail. 

Congratulations now we have successfully completed the file handling with selenium python. 

Output:

If you have any queries comment them down. We will solve that problem together like we just solved one. Also, if you have any suggestions then let me know. I will implement that in our next article. Also, don’t forget to share the article with your friends. Follow our pages on LinkedIn, Instagram, and Facebook. and subscribe to our blog. So, whenever we post some amazing content you will get to know it and, you will not have to wait for it. 

For Reference: https://pynative.com/python/file-handling/

Conclusion:

In my opinion, validation of file downloading at a particular location is a very easy process. Only, if you have the right solution for reference. In this article, I am sure I have provided the right solution for all your file-downloading problems and validations.

Read more blogs here

Using Visual Studio Code for Behave BDD tests in Python

Using Visual Studio Code for Behave BDD tests in Python

Most of us use PyCharm to automate the BDD tests using Behave using Python, however, it adds to the cost of test automation. The community version is also available for free, but we can’t run behave files through it. Visual Studio Code also supports Python. However, this blog explains how to use Visual Studio Code for Python + Behave BDD tests for FREE. PyCharm and VS Code are both excellent tools for writing Python code.

PyCharm Community edition and Visual Studio Code (VSCode) are both capable integrated development environments for Python coding. To conduct this evaluation, I chose the most frequently used IDE feature and compared the experience of PyCharm and VSCode.

Let’s see how can we use VSCode for “Python behave” Automation:

First, you should make sure that you have installed the Python extension. I recommend installing it because it will allow you to install libraries and run code. To run Python and Python behave in Visual Studio Code (VSCode), visit the Visual Studio Code Marketplace to get all the extensions you’ll need.

The following three extensions must be installed before proceeding to Python:-
  • Gherkin- for creating feature files
  • Pylance: This will install all libraries which are required to run Python
  • Python extension for VSCode
All of these extensions are available in the VSCode Marketplace, and you can install them manually.
  • Firstly, we have to create a launch.json file. From the “Run and Debug” option & this file can be generated.·      
  • Go to the “Run and Debug” option, where you will see “Create a launch.json file,” and put the below code into that file.

For reference, visit: https://go.microsoft.com/fwlink/?linkid=830387

{
  "version": "0.2.0",
  "configurations": [
{"name":"Python:Current File",  // You can change this name as per your convenient
"type":"python",
"request":"launch",
"program":"${file}",
"console":"integratedTerminal",
"justMyCode":true
},
 {
    "version": "0.2.0",
    "configurations": [
        {"name":"Python: Current File","type":"python","request":"launch","program":"${file}","console":"integratedTerminal","justMyCode":true},
        {
            "name": "Python: Behave current file",
            "type": "python",
            "request": "launch",
            "module": "behave",
            "console": "integratedTerminal",
            "env": {
                "BASE_DIR": "Your repository location",
                   },
            "args":[
                "--no-capture",
                "--no-capture-stderr",
                "${file}"
            ],
        }
    ]
}

With this code, you will be able to create a “launch.json” file that will aid in running Python behavior in Visual Studio Code. As soon as you are done with the “launch.json” file, you need to add some libraries to the “settings.json” file, which will help you run the code through the command line. Open a settings JSON file by pressing (ctrl + shift + p) and selecting “Open Settings.” JSON is an option.

Add the below code to the “Settings.json” file. The code below will assist users in integrating with GitHub and running files via the command line.

"terminal.external.windowsExec":"C:\\Program Files\\Git\bin\\bash.exe",
    "terminal.integrated.shell.windows":"C:\\ProgramFiles\\Git\bin\\bash.exe",
    "python.pythonPath":"D:\\testing-native\\venv",    

The final line of code, “python.pythonpath,” will show the virtual environment path. For instructions on how to set up a virtual environment for this, see https://code.visualstudio.com/docs/python/environments Now, the user can go to the “feature file” that he/she wanted to run by selecting “Run” from the “Run and Debug” option.

Cloud Environment:

To automate an application in a cloud environment like (BrowserStack/SauceLabs), add the following code to the “Settings.json” file. Users will be able to run code in a cloud environment. Execution mode determines which cloud environment the user wishes to automate the application in, whether it is BrowserStack or SauceLabs. You can run your tests on the cloud environment of your choice by using the username and access key provided.  

	"executionMode":"Browserstack/Saucelabs",
        "userName":"Your username Key",
        "accessKey":"Your acess key"
 Navigate to Specific Function or Method.

We’d all like to be able to navigate directly to that specific function or method from the feature file step, as we can in PyCharm (ctrl+click). This can be achieved by including the following code in Settings.json in VSCode: Your work in VS code will be faster and more flexible.  

"cucumberautocomplete.steps": [
        "Features/steps/*.py"
                              ],
    "cucumberautocomplete.syncfeatures": "Features/*feature",
Conclusion:-

Pycharm is used to run a Python behave file, but many of us are aware that it’s not free, so we’ve found a solution with Visual Studio Code that will help people save money and run Python behave freely on the VSCode IDE.

Read more blogs here