Introduction
Nowadays, Python is a very popular programming language. We can use Python for developing complex features, like machine learning and data analysis. Also, we can develop complex web applications using Python-based frameworks like Django. Please find below the image to get a more clear understanding of the frameworks available in Python.
Apart from this, we can develop a good web automation framework using Python and Selenium.
Everyone is aware of Selenium that is used to automate any web application. So, there are so many programming languages available with Selenium like Java, JavaScript, and then C#.
In this document, we will try to automate one website with Python and Selenium and try to understand how Python is better or simpler than other languages when it comes to developing an automation framework.
Environment Setup
First, we will start with some basic downloading and environment setup.
Download Python
Open the Python
website and download the latest version and install it on your local machine.
Download Selenium Standalone Server
Currently, version 3.141.59 is the current version.
This is basically a zip file. After unzipping it, you will get a jar file. A jar file is the executable Java file. To run this, open a command prompt and run java -jar selenium-server-standalone-3.x.x.jar
To run this command, you need Java. If Java is not available, then please go ahead and install Java on your machine. After the successful execution, you will get the below screen.
Download Chrome Driver - As per your preferred browser, you can download the driver on your local machine. From
here, you will get the Chrome Driver.
Now, open the IDE for development purposes. There is n number of options available here. I am using pyCharm. If you want to download and install, then please follow this
website.
I have opened pyCharm and created a project.
At the bottom, you will find one terminal tab. By selecting that tab, open the terminal and execute the below command.
pip install selenium
This will download and add Selenium libraries on your local system so that in any application, you can easily import the Selenium libraries.
Now, create or open any Python file, say sample.py.
sample.py
- from selenium import webdriver
- from selenium.webdriver.common.keys import Keys
-
- driver = webdriver.Chrome(chrome_driver_folder_path\chromedriver')
- driver.get("http://www.python.org")
- assert "Python" in driver.title
- elem = driver.find_element_by_name("q")
- elem.clear()
- elem.send_keys("pycon")
- elem.send_keys(Keys.RETURN)
- assert "No results found." not in driver.page_source
- driver.close()
If you copy the above code and paste in sample.py file is also fine. Only change the chrome_driver_folder_path
with valid folder path where the Chrome driver is downloaded and stored.
Finally, run the application.
In a terminal window, type "python sample.py" and press Enter.
Your application starts running and showing the Python website.
This is very very simple to start with Python with Selenium. But when we are going for automating large websites, we do require to design a framework with some features.
Summary
In this article, we understood the simplest way to automate a web application using Selenium and Python. In the next article, we will try to build a proper framework using Python with Selenium.