Python With Selenium

In this article, you will learn about Python with Selenium. Also, we will see why Python is better for developing an automation framework.

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.
 
Python With Selenium 
 
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.
 
Python With Selenium 
 

Download Selenium Standalone Server

 
Open the Selenium website to download the Selenium Standalone Server.
 
Python With Selenium 
 
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.
 
Python With Selenium 
 
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.
 
Python With Selenium 
 
Now, open the IDE for development purposes. There are n number of options available here. I am using pyCharm. If you want to download and install, then please follow this website.
 
Python With Selenium 
 
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.
 
Python With Selenium 
 
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
  1. from selenium import webdriver  
  2. from selenium.webdriver.common.keys import Keys  
  3.   
  4. driver = webdriver.Chrome(chrome_driver_folder_path\chromedriver')  
  5. driver.get("http://www.python.org")  
  6. assert "Python" in driver.title  
  7. elem = driver.find_element_by_name("q")  
  8. elem.clear()  
  9. elem.send_keys("pycon")  
  10. elem.send_keys(Keys.RETURN)  
  11. assert "No results found." not in driver.page_source  
  12. 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.
 
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.