• Selenium Video Tutorials

Selenium with Python Tutorial



Selenium is used for developing test cases for mostly web based applications. It can be used with various programming languages like Java, Python, Kotlin, and so on.

Setup Selenium with Python and Launch Browser

Step 1 − Download and install Python from the link Latest Version for Windows.

To get a more detailed view on how setup Python, refer to the link Python Environment Setup.

Once we have successfully installed Python, confirm its installation by running the command: python version, from the command prompt. The output of the command executed would point to the Python version installed in the machine.

Step 2 − Install the Python code editor called the PyCharm from the link PyCharm.

Using this editor, we can start working on a Python project to start our test automation. To get a more detailed view on how to set up PyCharm, refer to the below link Pycharm Tutorial.

Step 3 − Run the command: pip install selenium from the command prompt. This is done to install the Selenium. To confirm the version of Selenium installed in the machine, run the command −

pip show selenium

The output of this command gave the below result −

Name: selenium
Version: 4.19.0
Summary: None
Home-page: https://www.selenium.dev
Author: None
Author-email: None
License: Apache 2.0.

Step 4 − Restart PyCharm once Step 3 has been completed.

Step 5 − Open PyCharm and create a New Project by navigating to the File menu.

Selenium Python Tutorial 1

Enter the project name and location within the Location field, and then click on the Create button.

Selenium Python Tutorial 2

Step 6 − From the right bottom corner of the PyCharm editor, select the option Interpreter Settings.

Selenium Python Tutorial 3

Select the option Python Interpreter from the left, then click on '+'.

Selenium Python Tutorial 4

Step 7 − Enter selenium in the package search box inside the Available Packages popup, then the search results appear along with the Description to the right. The Description contains information about the version of the Selenium package that shall be installed.

There is also an option to install a specific version of the Selenium package beside the Specify version field. Then click on the Install Package button. After successful installation, the message Package 'selenium' installed successfully should display.

Selenium Python Tutorial 5

Step 8 − Enter webdriver-manager in the package search box inside the Available Packages popup, and install it in the same way.

Selenium Python Tutorial 6

Exit out of the Available Packages popup.

Step 9 − Both the packages selenium, and webdriver-manager should reflect under the Package. Click on the OK button. Restart PyCharm.

Selenium Python Tutorial 7

Step 10 − Create the first test case, by right clicking on the Project folder. Here, we have given the project name as SeleniumPython. Then click on New and finally click on the Python File option.

Selenium Python Tutorial 8

Step 11 − Enter a filename, say Test1.py and select the option Python File, finally click on Enter.

Selenium Python Tutorial 9

The Test1.py should appear in the left under the SeleniumPython project folder.

Selenium Python Tutorial 10

Step 12 − Open the newly created Python file with the name Test1.py, and obtain the page title - Selenium Practice - Alerts of the below page.

Selenium Python Tutorial 11

Example

from selenium import webdriver

# create instance of webdriver
driver = webdriver.Chrome()

# launch application
driver.get("https://www.tutorialspoint.com/selenium/practice/alerts.php")

# get page title
print("Page title is: " + driver.title)

# quitting browser
driver.quit

Output

Page title is: Selenium Practice - Alerts

Process finished with exit code 0

In the above example, we had launched an application and obtained its page title in the console with the message - Page title is: Selenium Practice - Alerts.

The output shows the message - Process with exit code 0 meaning that the above code executed successfully.

Identify Element and Check Its Functionality using Selenium Python

As an application gets launched, the users perform actions on the web elements on the web page like clicking on a link or a button, entering text within an input box, submitting a form, and so on for automating the test cases.

The first step is to locate the elements. There are various locators available in Selenium like the id, class name, class, name, tagname, partial link text, link text, tagname, xpath, and css. These locators are used with the find_element() method in Python.

For example, driver.find_element("classname", 'btn-primary') locates the first element with the class name attribute value as btn-primary. In case there is no element with that matching value of the class name attribute, NoSuchElementException is thrown.

Let us see the html code of the Click Me button highlighted in the below image −

Selenium Python Tutorial 12

Its class name attribute with value btn-primary. Click on Click Me button, post that the text You have done a dynamic click appears on the page.

Selenium Python Tutorial 13

Example

from selenium import webdriver
from selenium.webdriver.common.by import By

# create instance of webdriver
driver = webdriver.Chrome()

# implicit wait of 15 seconds
driver.implicitly_wait(15)

# launch application
driver.get("https://www.tutorialspoint.com/selenium/practice/buttons.php")

# identify element with class name
button = driver.find_element(By.CLASS_NAME, 'btn-primary')

# click on button
button.click()

# identify element with id
txt = driver.find_element(By.ID, 'welcomeDiv')

# get text
print("Text obtained is: " + txt.text)

# quitting browser
driver.quit

Output

Text obtained is: You have done a dynamic click

We had retrieved the text after clicking Click Me with the message in the console - Text obtained is: You have done a dynamic click.

Conclusion

This concludes our comprehensive take on the tutorial on Selenium Python Tutorial. Weve started with describing how to set up Selenium with Python and launch a browser, and how to identify an element and check its functionality using Selenium Python. This equips you with in-depth knowledge of the Selenium Python Tutorial. It is wise to keep practicing what youve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.

Advertisements