5 Selenium tricks to make your life easier

On This Page Trick # 1 & # 8211; Launching multiple tests with same parametersJanuary 21, 2026 · 7 min read · Tool Comparison

5 Selenium tricks to make your life easier

Selenium WebDriver is wide used for test automation, and knowing its advanced tricks can promote productivity and script efficiency. From handling waits to capturing screenshots and work with dynamical elements, these tricks make mechanization quicker and more reliable. Chances are that you use Selenium WebDriver as the go-to model for your web automation motive. It is sure one of the most popular test automation fabric in existence.

Overview

Launching multiple tests with like argument

  • Use TestNG to define specific argument for a examination retinue in the configuration file.

Passing Simple Values to a Test

  • Use the DataProvider method to retrovert a two-dimensional array of objective.

Handle Pop-up Windows

  • Use the switch_to method to manage pop-ups.

Handle Dynamic Content

  • Dynamic Elements can be deal with the help of Selenium waiting bidding.

Handle Drop-Down Menu

  • Quality class is used to choose and deselect option in a dropdown.

This article has taken the liberty of listing five tricks that you plausibly didn ’ t know you could do with your Selenium trial cases.

Trick # 1 & # 8211; Launching multiple tests with same argument

This is for suit in in which the like values receive to be used in diverse tests. Use TestNG capacity to define specific parameters for a test suite in the conformation file.

In the example below, the code is directed to provide the URL for the WebDriver method in order to execute a test suite. The test has been written in theTestNG framework.

public class TestNGParameterExample {@ Parameters ({`` firstSampleParameter ''}) @ Test (enabled = true) public void firstTestForOneParameter (String firstSampleParameter) {driver.get (firstSampleParameter); WebElement searchBar = driver.findElement (By.linkText (`` Mobile Device & amp; Browser Lab '')); searchBar.click (); Assert.assertEquals (driver.findElement (By.tagName (`` h1 '')) .getText (), `` Mobile Device & amp; Browser Lab '');} @ Parameters ({`` firstSampleParameter ''}) @ Test public void secondTestForOneParameter (String firstSampleParameter) {driver.get (firstSampleParameter); WebElement searchBar = driver.findElement (By.linkText (`` Live Cross Browser Testing '')); searchBar.click (); Assert.assertEquals (driver.findElement (By.tagName (`` h1 '')) .getText (), `` Cross Browser Testing '');}} testng.xml file structure is shown below. & lt;! DOCTYPE rooms SYSTEM `` https: //testng.org/testng-1.0.dtd '' & gt; & lt; suite name= '' Suite1 '' verbose= '' 1 '' & gt; & lt; parameter name= '' firstSampleParameter '' value= '' https: //experitest.com/ '' / & gt; & lt; test name= '' Nopackage '' & gt; & lt; classes & gt; & lt; category name= '' TestNGParameterExample '' / & gt; & lt; /classes & gt; & lt; /test & gt; & lt; /suite & gt;

Remember that all parameterized tests get to be initiated with a constructor such asMaven.

Try Selenium Automation Testing for Free

Trick # 2 & # 8211;Passing Elementary Values to a Test

In this suit, the objective is to create exam that enable the processing of data sets. To do this, use the DataProvider method, which is capable of returning a two-dimensional raiment of objects.

  1. The first array dimension specify the number of trial launching.
  2. The 2nd dimension corresponds to the number and types of test value.

To translate this farther, canvass the code below which expend the annotation@ DataProvider

@ DataProvider () world Object [] [] listOfLinks () {regress new Object [] [] {{`` Mobile Device & amp; Browser Lab '', `` Mobile Device & amp; Browser Lab ''}, {`` Live Cross Browser Testing '', `` Cross Browser Testing ''}, {`` Automated Mobile Testing '', `` Mobile Test Automation! ``},};}

For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users.

The codification below points to the data supplier method that has the corresponding name.

@ Test (dataProvider = `` listOfLinks '') public void firstTestWithSimpleData (String linkname, String header) {driver.get (`` https: //experitest.com/ ''); WebElement searchBar = driver.findElement (By.linkText (linkname)); searchBar.click (); Assert.assertEquals (driver.findElement (By.tagName (`` h1 '')) .getText (), header);}

Trick # 3 & # 8211; Handle Pop-up Windows

A common challenge in Selenium web automation is interacting with pop-up windows. These window look mainly in three formats:

  1. Simple alert: displays a message
  2. Confirmation alert:requests user to confirm some operation
  3. Prompt alerting: informs the user that they need to input some data

Read More:

While WebDriver can not handle Windows-based alerts, it can deal web-based alerts. Use theswitch_tomethod to manage pop-ups.

& lt;! DOCTYPE html & gt; & lt; html & gt; & lt; body & gt; & lt; h2 & gt; Demo for Alert & lt; /h3 & gt; Clicking below button & lt; button onclick= '' create_alert_dialogue () '' name = '' submit '' & gt; Alert Creation & lt; /button & gt; & lt; script & gt; mapping create_alert_dialogue () {alert (`` Simple alert, please click OK to have '');} & lt; /script & gt; & lt; /body & gt; & lt; /html & gt;

Displayed below is an instance in which theswitch_tomethod is used. In this case, switch.to.alert is used to switch to the alert pop-up box. Once this happens, the alert is accepted with thealert.accept () method.

from selenium import webdriver import time from time significance nap from selenium.webdriver.support.ui import WebDriverWait from selenium.common.exceptions spell NoSuchElementException from selenium.common.exceptions spell TimeoutException from selenium.webdriver.support signification expected_conditions as EC from builtins import str driver = webdriver.Firefox () driver.get (`` file: // & lt; HTML File location & gt; '') driver.find_element_by_name (`` submit '') .click () nap (5) alert = driver.switch_to.alert text_in_alert = alert.text mark (`` The alert content is `` + text_in_alert) sleep (10) alert.accept () print (`` Alert examination is complete '') driver.close ()

Test Pop Up on Real Device Cloud for Free

Trick # 4 & # 8211; Handle Dynamic Content

Most present-day websites contain content that is dynamic in nature. This mostly applies to AJAX based apps.

A common example of this is an e-commerce site. Such sites tend to show different product to different users, depending on the user ’ s localization or their previous product selection. When examine a site like this, it is important to ensure that WebDriver continue prove when the full content of a page has charge completely.

Elements on dynamic Page oft load at different time intervals, issues may originate if an element is not yet present in the DOM. However, this challenge can be resolved quite effectively with the use ofSelenium wait commands.

By using Explicit Wait, a tester can direct WebDriver to pause test performance until a certain condition is met. Use it along with thethread.sleep ()use if the condition is to wait for an exact period of clip.

In the example below, two searches occur on the URL. The initiatory search is for the elementhome-btn. Here, the constituent is locate viaCLASS_NAMEfor a maximum duration of 5 seconds.

The second search looks for a clickable element login for a maximum interval of 5 s. If the element is present, a click () action is executed.

Both instances use WebDriverWait along with.

from selenium import webdriver from clip signification sleep from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.common.exceptions import TimeoutException from selenium.webdriver.support.ui significance WebDriverWait driver = webdriver.Firefox () driver.get (`` https: //www.browserstack.com/ '') try: myElem_1 = WebDriverWait (driver, 5) .until (EC.presence_of_element_located ((By.CLASS_NAME, 'home-btn '))) # element = driver.find_element_by_partial_link_text (`` START TESTING '') print (`` Element 1 found '') myElem_2 = WebDriverWait (driver, 5) .until (EC.element_to_be_clickable ((By.CLASS_NAME, 'login '))) print (`` Element 2 found '') myElem_2.click () sleep (10) # except NoSuchElementException: except TimeoutException: print (`` No element plant '') slumber (10) driver.close ()

Trick # 5 & # 8211; Handle Drop-Down Menu

Let ’ s begin by understand the basics.

The Selectclass is used by Selenium WebDriver to take and deselect options in a dropdown. Objects of Select type can be initialize by passing the dropdown WebElement as a parameter to its constructor.

Read More:

Example:

WebElement testDropDown = driver.findElement (By.id (`` testingDropdown '')); Select dropdown = new Select (testDropDown);

There are three slipway to select an option from the drop-down menu-

1. selectByIndex& # 8211; Used to select an alternative based on its index number, beginning with 0.

dropdown.selectByIndex (5);

2.selectByValue& # 8211; Used to select an option ground on its & # 8216; value & # 8217; dimension.

dropdown.selectByValue (`` Database '');

3. selectByVisibleText& # 8211; Used to select an option based on textbook over the selection.

dropdown.selectByVisibleText (`` Database Testing '');

Try Handling Drop Down using Selenium for Free

Use the proficiency delineate above to make your Selenium tests easier, more organized and less time-consuming. Additionally, it is significant to opt a Selenium-based platform that facilitates automated testing. Look out for tools that allow for testing on a, such as BrowserStack. As a testing tool, it is designed to make testers ’ lives easygoing, providing developer-centric resourcefulness such as in-built dev tools and integrations with democratic programming languages and framework. Consequently, it makes quicker, smoother and more result-oriented.

Tags
12,000+ Views

# Ask-and-Contributeabout this topic with our Discord community.

Related Guides

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free