Selenium’s Python Module is built to perform automated testing with Python. Selenium Python bindings provides a simple API to write functional/acceptance tests using Selenium WebDriver. Through Selenium Python API you can access all functionalities of Selenium WebDriver in an intuitive way. This article illustrates about how to use Selenium Python to write automated tests using Python Selenium.
If you have not installed Selenium and its components yet, install them from here – Selenium Python Introduction and Installation. The selenium package itself doesn’t provide a testing tool/framework. One can write test cases using Python’s unittest module. The other options for a tool/framework are py.test and nose.
How to write tests using Selenium in Python
We have used unittest framework of Python to write tests. Let’s test search functionality at Python.org using Python selenium tests. To know more about unittest, visit – unittest Documentation. Explanation for each line is given in code itself.
Code –
Python3
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class PythonOrgSearch(unittest.TestCase):
def setUp( self ):
self .driver = webdriver.Firefox()
def test_search_in_python_org( self ):
driver = self .driver
driver.get("http: / / www.python.org")
self .assertIn("Python", driver.title)
elem = driver.find_element_by_name("q")
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
def tearDown( self ):
self .driver.close()
if __name__ = = "__main__":
unittest.main()
|
Output –

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
06 Mar, 2023
Like Article
Save Article