Sitemap
Priyadarshani Pandey

Priyadarshani Pandey

Test Consultant, Writer, AI Enthusiast and Technophile, Check out my website:- https://www.techconceptz.com/

Solving Common Selenium Python Challenges

3 min readSep 2, 2024

Test automation and quality assurance (QA) continue to be highly valued skills in the IT sector. Despite significant advancements in AI, a substantial portion of test automation remains to be addressed by this technology. AI can analyze extensive historical data on test executions and test cases, identifying patterns, prioritizing test cases and severity, and documenting test cases at a fundamental level. However, exceptional Quality Engineers are still crucial for testing intricate business scenarios, replicating edge cases, automating test cases, constructing build jobs, and more.

Press enter or click to view image in full size
Computer screen with a coding tool opened
Photo credits to Luca Bravo

Selenium consistently tops the charts in the test automation field. It supports a variety of programming languages, including JAVA, Python, Ruby, JavaScript, and C#. Each new release of Selenium enhances test engineers’ capabilities, making them more efficient in automating a broad range of tests. This encompasses web and mobile application testing, and Selenium 4 introduces exciting libraries for testing browser developer tools commands, enabling the capture of extensive backend data.

This blog will explore some intriguing Selenium commands in the Python language. Python has become the favored language for test automation among developers and testers in the corporate world.

Some details about the test environment:-

  1. Selenium Version — 4.22
  2. Python Version — 3.10
  3. Pytest Version — 8.2.2
  4. Test Environment Specifications — Windows 11 + Pycharm Community Edition
  5. Application URL — http://the-internet.herokuapp.com

We will concentrate on some complex tests, utilizing a sample web application and crafting Python tests with Selenium and AutoIt. This will provide insights into tackling various automation challenges. Pytest is employed to organize and systematize the execution of tests.

Get Priyadarshani Pandey’s stories in your inbox

Join Medium for free to get updates from this writer.

A). The “setup.py” file is going to contain the method for test setup and teardown.

#Performing test setup
@pytest.fixture
def setup(environment_details):
print("Setting up...")
driver = webdriver.Firefox()
driver.get(environment_details)
yield driver
print("Tearing down...")
driver.close()

B). Basic Authentication — We do 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.

#Doing basic authentication
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

C). Drag and Drop — We will navigate to the “Drag and Drop” link and then drag element A onto element B.

#Method to drag and drop element
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()

D). Using JavaScript to manipulate web elements — We naviagate to the “Dynamic Loading” link where we navigate to a page with a hidden element on it. The display attribute of the element is then altered using a JavaScript executor to make it visible.

#Method to navigate to a page with hidden element and displaying it
def test_dynamic_loading(setup):
setup.find_element(By.LINK_TEXT,"Dynamic Loading").click()
setup.find_element(By.LINK_TEXT,"Example 1: Element on page that is hidden").click()
hidden_element = setup.find_element(By.XPATH,"//*[@id='finish']")
display_status = setup.find_element(By.XPATH,"//*[@id='finish']").get_attribute("style")

# Check elements display status
display_status == 'display: none;'
if display_status == 'display: none;':
# Use javascript to change the attribute display from none to block
setup.execute_script("arguments[0].setAttribute('style','display: block;')",hidden_element)
wait = WebDriverWait(setup,20).until(EC.presence_of_element_located((By.XPATH,"//*[@id='finish']")))

E). Moving outside browsers viewport using AutoIt alongwith Selenium — We navigate to the “Exit Intent” link, where a modal dialog appears as soon as the browser viewport is exited. This resembles the subscription boxes commonly seen on websites. Since Selenium lacks the ability to interact outside the browser’s viewport, the issue is addressed by employing a combination of AutoIt commands alongside Selenium.

# Method to exit the browser viewport to get a modal dialog appear.
def test_exit_intent(setup):
setup.find_element(By.LINK_TEXT,"Exit Intent").click()
window_size = setup.get_window_size()
# Capturing the window width and height
width = window_size['width']
height = window_size['height']

# Adjusting screen size metrics to move mouse to desired position
width_click = width-1000
height_click = height - (height-5)

# AIT command to move the mouse
ait.move(1000, 5)
modal = setup.find_element(By.XPATH,"//*[@id = 'ouibounce-modal']")
if modal.is_displayed():
print("Modal dialog is opened")
else:
print("Failed")

A comprehensive blog on Selenium Commands Reference can be found at Selenium Python Commands Reference (techconceptz.com).

Selenium Python Commands Reference — Part 2 (techconceptz.com)

With over 14 years of experience as an automation engineer and a product quality owner, I possess extensive expertise in resolving automation-related issues. If you encounter quality roadblocks or have business use cases that demand meticulous testing with precision and cost-effectiveness, feel free to contact me.

Email — priyadarshani.pandey@techconceptz.com

Linkedin — Priyadarshani Pandey | LinkedIn

--

--

Priyadarshani Pandey
Priyadarshani Pandey

Written by Priyadarshani Pandey

Test Consultant, Writer, AI Enthusiast and Technophile, Check out my website:- https://www.techconceptz.com/