SELENIUM BASICS:


SELENIUM WEBDRIVER:


TESTNG:


REPORTS:


BUILD & CONTINUOUS INTEGRATION TOOLS:


CROSS BROWSER TESTING:


MOBILE TESTING:


AUTOMATION FRAMEWORKS:


API Testing:


INTERVIEW QUESTIONS:


SAMPLE RESUME:

Selenium Python Tutorial For Beginners

Selenium Python Tutorial

Earlier we have learned Selenium using Java and in this post, we will learn Selenium Python.

Most of the companies looking for automation testers who have knowledge in Selenium WebDriver using Python.

In this Selenium Python tutorial, you will learn the following

I hope at the end of this tutorial, you will be able to write and execute Selenium test scripts using Python Programming language in different web browsers.

Most newcomers to automation testing ask me this question “Why choose Python over Java in Selenium”. Selenium with Java is popular in the market but to say in favor of Python Webdriver over Java are.

1. Learning Python is easy compared to Java.
2. Python codes are shorter compared to Java.
3. Python is more productive compared to Java because codes can be written in fewer lines.

How To Install Python:

Let’s see the installation of Python.

The following steps need to be followed for the configuring Python and Selenium in our machine:

Step 1: The python.exe file has to be obtained from the official site: 

https://www.python.org/downloads/

Python Download

Then click on the Download Python button. The python executable file gets downloaded with its latest version. If we are looking for a specific python version, then that will also be available on their official website.

Release Versions

If we are on other operating systems like Linux / UNIX, MAC, and so on, we should click on that specific link and the corresponding page should be navigated. For example for Linux / UNIX user, we are redirected to that specific page:

Latest Source Release

Step 2: Once the .exe file gets downloaded, we need to proceed with the next steps until the installation is completed. Then we can move to the location where the python got installed and check it.

Generally the path:

Step 3: For the Windows users only, we have to set the path of System Environmental variables. Here we have to add the location of the Python folder [path where it got installed in our local system]. Also, there will be a Scripts folder inside the main Python folder. We have to set that path as well in System Environmental variables.

Step 4: To verify that Python has been successfully installed, we need to run the command: python –version

Python Version Command Line

In case, the proper installation has not happened, error messages shall be displayed.

Step 5: Next we have to install Selenium bindings in our system. This will be achieved with the help of PIP Python has a package manager which helps to install and configure the required packages which are not included in the Python library.

Step 6: We will visit the site: https://pypi.org/project/selenium/ for getting all the necessary resources for setting up Selenium with Python in our system.

pip Install Selenium

Step 7: As suggested by the official web site of Python, we need to run the command pip install selenium from the command prompt. We can now find a selenium folder created inside the Python folder. [Python ->Lib ->site-packages].

Step 8: If we want to upgrade the present version of the package, we need to run the command pip install -U selenium from the command prompt. To confirm if Selenium has installed successfully, we need to run the command pip show Selenium. If there are multiple versions of Python in our system, we need to run the command pip3 show Selenium. This will display all the information like version, location and so on.

Verify Selenium Version

Step 9: Finally we shall download the PyCharm editor [developed by JetBrain] in our system to code for our automation. The link to download from is :

https://www.jetbrains.com/pycharm/download/#section=windows

There are both licensed [Professional] and free [Community] versions of PyCharm available. For our testing purpose, the Community version is sufficient.

Step 10: After installation of PyCharm, we need to create a new project from File -> New Project -> <<Provide the name of project>>. Then click on the Create While creating the project we shall select Virtualenv for the New Environment Using field. Also, we have to select a Basic interpreter. Here we should select the latest Python interpreter available in our system.

Next, we have to create a new Python package by selecting the Project that we created and finally a Python file inside the package.

To verify if the Selenium packages are available in our project, let’s open the External Libraries folder. Then if we move to the site-packages folder, we shall find selenium libraries are added. The other Python libraries shall also be present there.

External Libraries Path

How to run tests in various browsers in Selenium Python?

Selenium allows a wide range of browsers like Chrome, Firefox, Internet Explorer, and Safari, and so on for automation testing. For testing any application, we need to open a browser explicitly as Selenium does not have the ability to continue working on the already opened browser.

For running tests in the Chrome browser, we need to download the chromdriver.exe file from https://www.selenium.dev/ and save it on a location. In Python, we have to import the WebDriver library to our code with help from selenium import webdriver statement to launch the Chrome browser.

Then we have to create a driver instance. It shall contain the webdriver.Chrome () where we have to specify the location of the chromedriver.exe file. This is done by setting up the executable_path property to the Chrome class constructor.

Code Implementation to launch the Chrome browser.

For running tests in the Firefox browser, we need to download the geckodriver.exe file from https://www.selenium.dev/ and save it on a location. In Python, we have to import the WebDriver library to our code with help from selenium import webdriver statement to launch the Firefox browser.

Then we have to create a driver instance. It shall contain the webdriver.Firefox () where we have to specify the location of the geckodriver.exe file. This is done by setting up the executable_path property to the Firefox class constructor.

Code Implementation to launch the Firefox browser.

For running tests in the Internet Explorer browser, we need to download the IEDriverServer.exe file from https://www.selenium.dev/ and save it on a location.  In Python, we have to import the WebDriver library to our code with help from selenium import webdriver statement to launch the Internet Explorer browser.

Then we have to create a driver instance. It shall contain the webdriver.Ie() where we have to specify the location of the IEDriverServer.exe file. This is done by setting up the executable_path property to the Ie class constructor

Code Implementation to launch the Internet Explorer browser.

Basic WebDriver commands in Selenium Python

Let us discuss some of the common web driver commands used in Selenium Python.

get() – This command is used for launching an application. This command will execute until the page load is complete. Then the program flow moves to the next step of test execution.

current_url() – This command is used to fetch the URL of the application which is currently opened in the browser.

title () – This command is used to fetch the title of the application which is currently opened in the browser.

back() – This method is used to navigate back to the previously visited page and works only if we moved from one page to another page.

forward () – This method is used to navigate forward from a previously visited page and works only if back() is used prior to its used.

refresh () – This method is used to reload the existing page and refreshes it.

maximize_window () – This method is used to maximize the window of an existing page. Some applications open in a maximized window by default, applying maximize_window () method on them shall not have any impact on them

minimize_window () – This method is used to minimize the window of the existing page. Some applications open in minimized windows by default, applying minimize_window () method on them shall not have any impact on them.

close () – This method is used to close the window in focus. Suppose there are two open windows, the second window is active currently, close () method if applied shall close only the second window where Selenium has the control.

quit () – This method is used to close all the windows opened. Suppose there are two opened windows, the second window is active currently, quit () method if applied shall close all the driver sessions.

Advanced Selenium WebDriver Python Tutorial

We have also covered advanced topics of Selenium with Python. We will update the following topics along with advanced topics in the coming days. Bookmark this post to learn more.

You can also check the official selenium python documentation here.

Related Posts: 

Sharing is caring.

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

Like This Post?

We have a lot more where that came from?

We only send really good stuff occasionally, promise.

Rajkumar SM

Leave a Comment

Your email address will not be published. Required fields are marked *

Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on whatsapp

Recent Posts:

Scroll to Top
API Testing eBook

DOWNLOAD FOR FREE