New features are regularly added to web applications to boost user engagement. To ensure these updates work as intended and that the user interface remains functional, automated testing is crucial. Selenium is a widely-used tool for this type of automation testing.
Selenium is an open-source automation testing tool that supports various scripting languages such as C#, Java, Perl, Ruby, JavaScript, and others. The choice of scripting language can be made based on the specific requirements of the application being tested.
Python is one of the most popular choices when it comes to scripting with 51% of the developers using it, as suggested by the StackOverflow 2024 annual survey.
Table of ContentsDevelopers prefer Python for writing Selenium test scripts because of its simplicity, readability, and ease of use. Python’s clear and concise syntax allows for faster script development and easier maintenance, which is crucial in testing scenarios.
Additionally, Python has a rich set of libraries and frameworks that complement Selenium, making it easier to handle complex tasks such as data manipulation, reporting, and integration with other tools.
Python’s extensive community support and documentation also provide valuable resources for troubleshooting and improving test scripts. These factors make Python a popular choice for Selenium automation.
Getting started with Selenium using Python involves setting up an environment where you can write and run automated test scripts for web applications.
Selenium, combined with Python, offers a powerful and easy-to-learn toolset for automating browser interactions. Python’s simple syntax makes it ideal for quickly writing clear and maintainable test scripts.
To begin, you’ll need to install the Selenium WebDriver, set up a compatible browser, and learn the basics of locating web elements, interacting with them, and running test cases. This combination is perfect for testing dynamic and responsive web applications efficiently.
To run Selenium Python Tests here are the steps to follow:
Step 1. Import the Necessary Classes
First, you’ll need to import the WebDriver and Keys classes from Selenium. These classes help you interact with a web browser and emulate keyboard actions.
from selenium import webdriver from selenium.webdriver.common.keys import Keys
Step 2. Create a WebDriver Instance
To interact with a browser, you’ll need to create an instance of WebDriver. In this example, we use Chrome:
driver = webdriver.Chrome('./chromedriver')
Make sure chromedriver is in the same directory as your Python script. This command opens a new Chrome browser window.
Step 3. Load a Website
Use the .get() method to navigate to a website. This method waits for the page to load completely:
driver.get("https://www.python.org")
This will open Python’s official website in the browser.
Step 4. Check the Page Title
Once the page is loaded, you can retrieve and print the page title to verify you’re on the right page:
print(driver.title)
Welcome to Python.org
Step 5. Interact with the Search Bar
To perform a search, locate the search bar element, enter a query, and submit it. Here’s how to find the search bar by its name attribute and interact with it:
search_bar = driver.find_element_by_name("q") search_bar.clear() search_bar.send_keys("getting started with python") search_bar.send_keys(Keys.RETURN)
As an explanation :
Step 6. Verify the Resulting URL
After submitting the search query, you can check the updated URL to confirm the search results page:
print(driver.current_url)
You should see a URL similar to:
Step 7. Close the Browser
Finally, close the browser session to end the test:
driver.close()
Summary :
Here is the complete script for your first Selenium test in Python. Save this code in a file named selenium_test.py and run it using python selenium_test.py:
from selenium import webdriver from selenium.webdriver.common.keys import Keys # Create a new instance of the Chrome driver driver = webdriver.Chrome('./chromedriver') # Open the Python website driver.get("https://www.python.org") # Print the page title print(driver.title) # Find the search bar using its name attribute search_bar = driver.find_element_by_name("q") search_bar.clear() search_bar.send_keys("getting started with python") search_bar.send_keys(Keys.RETURN) # Print the current URL print(driver.current_url) # Close the browser window driver.close()
Selenium allows you to perform a variety of actions on web elements. You have already touched upon entering input, here’s how to interact with buttons, and dropdowns:
Assuming you want to click a button with the ID “submit-button” after entering the input in the search bar :
# Locate the button by its ID attribute button = driver.find_element_by_id("submit-button") # Click the button button.click()
If you need to click a link by its text:
# Locate the link by its link text link = driver.find_element_by_link_text("Click Here") # Click the link link.click()
Explanation:
Though dropdowns are not present on this site, they are quite common for web application testing
For dropdown menus, Selenium provides the Select class to handle options within elements.
Example: Selecting an Option from a Dropdown
Assuming you have a dropdown menu with the ID “dropdown-menu”:
from selenium.webdriver.support.ui import Select # Locate the dropdown menu by its ID attribute dropdown = Select(driver.find_element_by_id("dropdown-menu")) # Select an option by visible text dropdown.select_by_visible_text("Option 1") # Or select an option by value dropdown.select_by_value("option1") # Or select an option by index (0-based index) dropdown.select_by_index(0)
Explanation:
The HTML Document Object Model (DOM) represents the structure of a web page as a tree of objects. Selenium allows you to interact with these elements using various locator strategies.
In our first test script, we have already used some of the methods used to navigate DOM elements. This section will be a slightly more detailed view into how you can use different methods to locate and interact with elements on the Python.org website.
Example: Clicking the “Downloads” Link
To click the “Downloads” link, you can use the .find_element_by_link_text() method, but here’s how to use other locators to achieve the same, example by using find_element_by_xpath:
from selenium import webdriver # Set up the WebDriver driver = webdriver.Chrome('./chromedriver') # Open the Python website driver.get("https://www.python.org/") # Locate the "Downloads" link using XPath downloads_link = driver.find_element_by_xpath("//a[text()='Downloads']") # Click the "Downloads" link downloads_link.click() # Optionally, print the current URL to confirm navigation print(driver.current_url) # Close the browser driver.close()
Explanation:
XPath: //a[text()='Downloads']
locates the “Downloads” link based on its visible text.
Example: Accessing the Main Header
To access the main header text, you can use different locators to find the header element.
Using find_element_by_class_name:
from selenium import webdriver # Set up the WebDriver driver = webdriver.Chrome('./chromedriver') # Open the Python website driver.get("https://www.python.org/") # Locate the header element using its class name header = driver.find_element_by_class_name("introduction") # Print the text of the header print(header.text) # Close the browser driver.close()
Explanation:
Example: Filling Out and Submitting the Search Form
To interact with the search form, you can use the .find_element_by_name() method to locate the input field.
Using find_element_by_name:
from selenium import webdriver from selenium.webdriver.common.keys import Keys # Set up the WebDriver driver = webdriver.Chrome('./chromedriver') # Open the Python website driver.get("https://www.python.org/") # Locate the search bar using its name attribute search_bar = driver.find_element_by_name("q") # Clear any existing text and enter a new search term search_bar.clear() search_bar.send_keys("Python Documentation") search_bar.send_keys(Keys.RETURN) # Optionally, print the current URL to confirm search results print(driver.current_url) # Close the browser driver.close()
Explanation:
When working with multiple browser windows or tabs, or dealing with iframes (frames), you may need to switch contexts to interact with different elements.
Step 1. Handling Multiple Browser Windows or Tabs
Example: Switching Between Windows
To handle multiple browser windows or tabs:
from selenium import webdriver from selenium.webdriver.common.keys import Keys import time # Set up the WebDriver driver = webdriver.Chrome('./chromedriver') # Open the Python website driver.get("https://www.python.org/") # Open a new tab with a different URL driver.execute_script("window.open('https://www.google.com', '_blank');") # Switch to the new tab driver.switch_to.window(driver.window_handles[1]) # Perform actions in the new tab (e.g., search for 'Selenium') search_bar = driver.find_element_by_name("q") search_bar.clear() search_bar.send_keys("Selenium") search_bar.send_keys(Keys.RETURN) # Switch back to the original tab driver.switch_to.window(driver.window_handles[0]) # Close the browser driver.quit()
Explanation:
Step 2. Switching Between Frames
Example: Switching to an iFrame
To switch to and interact with elements within an iframe:
from selenium import webdriver from selenium.webdriver.common.keys import Keys import time # Set up the WebDriver driver = webdriver.Chrome('./chromedriver') # Open the Python website driver.get("https://www.python.org/") # Example site with iframe (replace with an actual URL that contains iframes) driver.get("https://www.w3schools.com/html/html_iframe.asp") # Switch to the iframe using its name or ID driver.switch_to.frame("iframeResult") # Perform actions within the iframe print(driver.find_element_by_tag_name("h1").text) # Switch back to the default content driver.switch_to.default_content() # Close the browser driver.quit()
Explanation:
Dynamic content can load at different times, so using waits helps ensure elements are present before interacting with them.
Step 1. Implicit Waits
Example: Using Implicit Waits
from selenium import webdriver # Set up the WebDriver driver = webdriver.Chrome('./chromedriver') # Set implicit wait driver.implicitly_wait(10) # seconds # Open the Python website driver.get("https://www.python.org/") # Locate an element with implicit wait search_bar = driver.find_element_by_name("q") search_bar.send_keys("Python") # Close the browser driver.quit()
Explanation:
Step 2. Explicit Waits
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Set up the WebDriver driver = webdriver.Chrome('./chromedriver') # Open the Python website driver.get("https://www.python.org/") # Define WebDriverWait with a maximum wait time of 10 seconds wait = WebDriverWait(driver, 10) # Wait for the search bar to be present in the DOM search_bar = wait.until(EC.presence_of_element_located((By.NAME, "q"))) # Perform actions on the search bar search_bar.send_keys("Python") # Close the browser driver.quit()
Explanation:
To ensure that the application behaves as expected, you can use assertions and validations.
Verifying Expected Conditions Using Assertions
Example: Verifying Page Title and Search Results
from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Set up the WebDriver driver = webdriver.Chrome('./chromedriver') # Open the Python website driver.get("https://www.python.org/") # Use WebDriverWait to wait for the search bar to be present wait = WebDriverWait(driver, 10) search_bar = wait.until(EC.presence_of_element_located((By.NAME, "q"))) # Perform search search_bar.send_keys("Python") search_bar.send_keys(Keys.RETURN) # Verify the title contains "Python" assert "Python" in driver.title # Verify search results contain expected text results = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "ul.list-recent-events"))) assert "Python" in results.text # Print the results to verify print(driver.title) print(results.text) # Close the browser driver.quit()
Explanation:
Web applications often use JavaScript alerts, confirmation dialogs, or prompts to interact with users. Selenium provides ways to handle these pop-ups effectively.
Dealing with JavaScript Alerts
JavaScript alerts are simple pop-up messages that require user interaction to dismiss. Selenium allows you to interact with these alerts using the switch_to.alert() method.
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys # Set up the WebDriver driver = webdriver.Chrome('./chromedriver') # Open a website that triggers an alert (example URL) driver.get("https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/Alert.html") # Click a button that triggers an alert trigger_alert_button = driver.find_element(By.ID, "alertButton") # Adjust locator as needed trigger_alert_button.click() # Switch to the alert and accept it alert = driver.switch_to.alert print("Alert text:", alert.text) alert.accept() # Close the browser driver.quit()
Explanation:
Other Alert Actions:
Properly closing the browser session is crucial for releasing resources and ensuring that your automation script runs cleanly.
Properly Closing the Browser Session
Example: Closing the Browser
from selenium import webdriver # Set up the WebDriver driver = webdriver.Chrome('./chromedriver') # Open a website driver.get("https://www.python.org/") # Perform actions (e.g., search) search_bar = driver.find_element(By.NAME, "q") search_bar.send_keys("Python") search_bar.send_keys(Keys.RETURN) # Cleanup: Close the browser driver.quit()
Explanation:
Alternative Methods:
Integrating Selenium tests with a testing framework provides structured test cases, reporting, and additional functionality such as setup and teardown methods.
unittest is a built-in Python testing framework that provides a structured approach to writing and running tests, including test case management, fixtures, and test discovery. Integrating Selenium with unittest allows for organized test cases, setup and teardown methods, and detailed test reports, making it easier to manage and maintain automated tests.
Example: Basic Test with unittest
import unittest from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys class PythonOrgSearchTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver = webdriver.Chrome('./chromedriver') cls.driver.get("https://www.python.org/") def test_search_python(self): search_bar = self.driver.find_element(By.NAME, "q") search_bar.send_keys("Python") search_bar.send_keys(Keys.RETURN) self.assertIn("Python", self.driver.title) @classmethod def tearDownClass(cls): cls.driver.quit() if __name__ == "__main__": unittest.main()
Explanation:
pytest is a powerful and flexible Python testing framework that simplifies writing tests with its rich feature set, including fixtures, parameterized tests, and detailed assertions. Integrating Selenium with pytest enhances test organization, facilitates advanced setup/teardown functionality, and generates comprehensive test reports, improving test reliability and clarity.
Example: Basic Test with pytest
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys @pytest.fixture(scope="module") def driver(): driver = webdriver.Chrome('./chromedriver') yield driver driver.quit() def test_search_python(driver): driver.get("https://www.python.org/") search_bar = driver.find_element(By.NAME, "q") search_bar.send_keys("Python") search_bar.send_keys(Keys.RETURN) assert "Python" in driver.title
Explanation:
Here are five best practices for using Selenium WebDriver with Python:
Running Selenium Python tests on BrowserStack’s Real Device Cloud offers numerous advantages that significantly enhance testing efficiency and effectiveness.
BrowserStack provides access to a wide range of real devices and browsers, ensuring that tests reflect real-world scenarios and uncover device-specific issues. The platform supports scalable parallel execution, allowing multiple tests to run simultaneously across various configurations, which accelerates the development cycle.
Cross-platform testing on BrowserStack ensures consistent application performance across different environments. Additionally, it offers real-time debugging features such as live logs, screenshots, and video recordings, which aid in quick troubleshooting.
Seamless integration with CI/CD pipelines further automates the testing process, enabling tests to run on every code change and providing immediate feedback on application quality. Overall, BrowserStack Automate enables comprehensive, efficient, and reliable testing, fostering continuous development and deployment.