In this tutorial, we will learn about the introduction to Selenium Python. We have learnt the selenium with Java in previous tutorial. Now we will check how to work with Python in Selenium.
Objective:
- Introduction to Selenium Python
- Python Installation
- Run First Python Code
Introduction:
Python is very easy language than Java. So it will be easier to work with Selenium if you have knowledge on Selenium.
If you are new to Python, Then have a look on our Python tutorial by clicking here and resume your leaning from here.
There are some important section of python you need here to work with Selenium is “Creation of Function”, “constructor”, “variable”, ”List”, ”Tuple”, “loop” etc. Let’s have a look on Selenium with Python.
Python Installation:
Now we are clear with the introduction to Selenium with Python. To work with Selenium with Python, You need to install Python in your system. To check whether python is stalled in your system or not, Go to command prompt and type:
“python --version”

If you will see the above message as shown in above image, then you can confirm that you have installed Python in your system successfully.
If you will not see the python version, then you need to install Python to your system. To install Python into your system, go to our Python installation tutorial by clicking this link.
Now after installation, you need to set up in your environment variable. To achieve that you need to go to the python installed folder and copy the location and paste to your environment path variable.



You need to add 2 path, one is upto script and another is python installed folde.
Now you are good to go to work with Python.
If you want to see the Python console from command prompt then you need to type python and enter.

Let’s write some code in Python console and run.

From the above example you can notice that, we have written
X=2
Y=3
X+Y
Once you enter, you will see the output as 5.
You can write your Selenium code here. But this is not recommended. For this you need a IDE for suggestion, Compile, Framework design etc. You can use PyCharm IDE to work with Selenium with Python.
To install PyCharm, Go to this link and install your PyCharm IDE and come here and resume your learning.
Run First Python Code:
Now open the PyCharm Tool:

Create the project in PyCharm.
Now you need to create a Project by clicking the File -> New Project and click on Create. In Eclipse, you can create the Maven Project or Java Project or Gradle Project etc., But in PyCharm Maven Project will not support in Python as Maven is the Java build automation tool.

Here a few things you need to understand while creating the project.
Location: This is the location where you will create the Python file.
New Environment: This defines the Environment where your python file will be executed. You can choose Virtual Environment for now
Location (Inside Environment): This is the location for your Python environment.
Base Interpreter: This is the location for python.exe file
It may take some time to set up or configure the virtual Environment. Once done we can see the blank window with Project name as below.

Configure the Project:
Like Selenium with Java, You can add the dependency through Maven from Maven boundary XML file and then pom.xml file will download all dependency libraries for your project.
But in Python Maven does not support. Then how to configure all libraries. For this we need pip command. You can expand the External Libraries in PyCharm and will see the pip is installed inside that.

To configure, right click on the project name and click on open in terminal.


Now you need to install / add libraries for Selenium into your project. For this, run below command
pip install –U selenium

Now you can notice that our selenium libraries are successfully added to our project. It will download selenium packages and configured your project as a selenium project. You can see your project is configured with selenium 3.141.0.
To verify whether it is added to your External libraries or not, you can navigate to External Libraries inside your project and expand site-packages.

You can see selenium is available inside the site-packages. And we are ready to work with Selenium now.
First Code:
Let’s write our first Python code here. To start with you need to follow below steps.
Create the Package:
Let’s start with our code write within the package. For this, you need to create the packages. If you want learn more about the packages then you can click this link and learn.
To create package, you need to right click on project name and choose Python package as below. Provide the package name inside the box and click Enter.


After this you can see a folder inside our project is created with the package name “Selenium” and inside this one default python file is created name __init__.py which notified that this is python project.
Create Python file:
Now Right click on the package and go New and create Python file.

Once you create the python file, you can see the Python file opened as below. For Python file, extension is .py which is added bydefault while creating the file. You do not need to specify the .py with the file name.

Write Python code:
Selenium will not come bydefault to the Python file. To work with Selenium, you need to import selenium as below.
from selenium import webdriver
This signifies that from selenium package we want to import the webdriver.
To specify, which browser you want to work with selenium, you need webdriver. Webdriver has a method called chrome() which is used to open the Chrome Browser. This will give the reference which will be used further.
driver = webdriver.Chrome()
To give executable path for Chrome driver you need to provide below argument inside the chrome()
Here driver is just a name of the reference, You can use any name other than driver.
Like Chrome, If you want to work with Firefox browser then you can use.
driver = webdriver.firefox()
driver.get("https://www.facebook.com/") print(driver.title)
Execute the file:
Let’s execute this. To execute the selenium file, there is 2 ways.
- You can go to the Terminal and navigate to the current location run below command
python filename.py
- Right click on the file and click on “Run”.


Summary:
In this tutorial, we have learned some introduction to Selenium Python and how to execute the first Python script.