Python is emerging as one of today's most popular programming languages. It is extensively used in AI and ML, as well as in developing web applications. Python is employed in creating everything from the simplest to the most complex and sophisticated systems, significantly influencing the QA testing landscape in the IT industry.
Many companies are transitioning their test automation frameworks from Java to Python. Python, known for its simplicity and requiring less coding, is rapidly becoming popular in the test automation field. Selenium, a widely used open-source tool for developing automation frameworks for applications, is now predominantly utilized with Python.
This blog is designed to offer the Python equivalents of the most commonly utilized Selenium commands. It is not a framework, but rather a collection of direct commands to help both existing and new Selenium Python users begin and familiarize themselves with the tool.
Test Environment
Selenium Version - 4.22
Python Version - 3.10
Pytest Version - 8.2.2
Test Environment Specifications - Windows 11 + Pycharm Community Edition
Application URL - http://the-internet.herokuapp.com/
Here is a screenshot of the test environment.
We now describe what we try to achieve in each test followed by the respective Selenium Python code snippet:-
This file contains the application URL defined in a single location, eliminating the need to manually enter it across various test cases for each test.
@pytest.fixture
def environment_details():
url = "http://the-internet.herokuapp.com/"
return url
Add_Remove_Elements.py
This file includes all the Selenium commands to be discussed in this article. The setup and yield commands have been incorporated into a pytest.fixture method. Subsequent to this, the test methods will be presented.
@pytest.fixture
def setup(environment_details):
print("Setting up...")
driver = webdriver.Firefox()
driver.get(environment_details)
yield driver
print("Tearing down...")
driver.close()
Adding and Removing Elements
This test involves navigating to the "Add/Remove Elements" link and attempting to add or delete web elements on the page.
def test_click_add_remove_elements(setup):
setup.find_element(By.LINK_TEXT,"Add/Remove Elements").click()
setup.find_element(By.XPATH,"//*[contains(text(),'Add Element')]").click()
setup.find_element(By.XPATH,"//*[contains(text(),'Delete')]")
Basic Authentication -
In this test, username and password-based authentication is performed by passing the credentials directly in the URL, without using the "Basic Auth" link provided on the website.
def test_basic_auth(setup):
setup.get("https://admin:admin@the-internet.herokuapp.com/basic_auth")
message = setup.find_element(By.XPATH,"//p").text
congrats = message.startswith("Congratulations")
assert congrats
Broken Images -
This test involves navigating to the "Broken Images" link to determine if a specific image is displayed. Additionally, we verify if the image possesses any attributes.
def test_broken_images(setup):
setup.find_element(By.LINK_TEXT,"Broken Images").click()
color = setup.find_element(By.XPATH,"//img[@src='asdf.jpg']").get_attribute("color")
if color == None:
print("Image is blank")
Challenging DOM -
In this test, we will navigate to the "Challenging DOM" link, traverse a table to locate a specific column value, and execute a click operation on it.
def test_challenging_dom(setup):
setup.find_element(By.LINK_TEXT,"Challenging DOM").click()
rows = setup.find_elements(By.XPATH,"//table/tbody/tr")
for row in rows:
column_eight = row.find_element(By.XPATH,"td[7]")
column_eight.find_element(By.LINK_TEXT,"edit").click()
Handling Checboxes -
In this test, we will navigate to the "Checkboxes" link and then verify the status of the checkboxes on the page. If a checkbox is already checked, we will leave it as is; otherwise, we will check the box if it is unchecked.
def test_checkboxes(setup):
setup.find_element(By.LINK_TEXT,"Checkboxes").click()
checkboxes = setup.find_elements(By.XPATH,"//*[@id='checkboxes']/child::input")
for cbox in checkboxes:
if cbox.get_attribute("checked") == None:
cbox.click()
Right Click or Context Click -
In this test, we will navigate to the "Context Menu" link. On the page, we will right-click on an image, which will trigger an alert. We will then accept the alert.
def test_context_menu(setup):
setup.find_element(By.LINK_TEXT,"Context Menu").click()
element = setup.find_element(By.XPATH,"//*[@id='hot-spot']")
ActionChains(setup).context_click(element).perform()
setup.switch_to.alert.accept()
Capture Disappearing Elements
In this test, we proceed to the "Disappearing Elements" link. We traverse an unordered list of elements and obtain the list of displayed elements.
def test_disappearing_elements(setup):
setup.find_element(By.LINK_TEXT,"Disappearing Elements").click()
elements = setup.find_elements(By.XPATH,"//ul/child::li")
for e in elements:
print(e.text)
Drag and Drop
In this test, we will navigate to the "Drag and Drop" link and then drag element A onto element B.
def test_drag_and_drop(setup):
setup.find_element(By.LINK_TEXT,"Drag and Drop").click()
source = WebDriverWait(setup,20).until(EC.element_to_be_clickable((By.XPATH,"//*[@id='column-a']")))
target = WebDriverWait(setup,20).until(EC.element_to_be_clickable((By.XPATH,"//*[@id='column-b']")))
ActionChains(setup).drag_and_drop(source,target).perform()
Dynamic Content -
In this test, we will navigate to the "Dynamic Content" link and capture attributes of dynamically displayed elements with same xpaths for various elements.
def test_dynamic_content(setup):
setup.find_element(By.LINK_TEXT,"Dynamic Content").click()
images = setup.find_elements(By.XPATH,"//*[@class='large-10 columns large-centered']/div/div/child::img")
texts = setup.find_elements(By.XPATH,"//*[@class='large-10 columns large-centered']/div/div/child::img/following::div[@class='large-10 columns']")
size = len(images)
for img in images:
print(img.get_attribute("src"))
for t in texts:
print(t.text)
We execute the tests and redirect the output to an XML file using the following command:
pytest -v --junitxml="results.xml"
Execution run is posted on link below:-
The comprehensive code base for the above reference has been checked in @
priyadarshanipandey/HerokuAutomation: A python selenium commands reference with demo site as https://the-internet.herokuapp.com/ (github.com)
Part 2 of this blog series can be found @
With over 14 years of experience as an automation engineer and a product quality owner, I possess extensive expertise in resolving automation-related challenges. If you encounter quality roadblocks or have business use cases that demand meticulous testing with precision and cost-efficiency, please consider reaching out to me.
You can reach me @
Linkedin - Priyadarshani Pandey | LinkedIn
Comments