Top Selenium Commands for Developers and Testers

Related Product On This Page What are Selenium Commands?February 10, 2026 · 14 min read · Tool Comparison

Related Product

Top Selenium Commands for Developers and Testers

Selenium commands help automatize browser activeness and test web applications efficiently. They form the foundation of any Selenium script, enabling developers and testers to feign user interactions and validate functionality.

Overview

What are Selenium Commands?

Selenium commands are methods provided by the Selenium WebDriver API to perform tasks like clicking elements, entering text, navigating Page, and verifying weather in a web browser.

Top Selenium Commands

Mastering these essential commands can greatly raise the speed and reliability of your test automation.

  1. driver.get (url)– Opens the specified URL in the browser.
  2. driver.findElement (By.locator)– Locates a individual web ingredient using a specified strategy (like ID, gens, or CSS picker).
  3. element.click ()– Simulates a mouse click on a web element.
  4. element.sendKeys (& # 8220; text & # 8221;)– Types the given comment into a text field or textarea.
  5. driver.navigate () .back ()– Navigates to the previous page in the browser history.
  6. driver.quit ()– Closes all browser windows and ends the WebDriver session.

This article explains what Selenium commands are and highlights the most usually used ones that are crucial for building efficacious automated exam playscript.

What are Selenium Commands?

commandsrefer to the predefined methods (such as getTitle (), getCurrentUrl (), click (), and more) provided by Selenium WebDriver that enables interaction with web element and control browser behavior during.

These commands help automatize respective browser tasks, such as navigating between tabloid and windows, tick on buttons or other elements, getting the current page rubric or URL, entering textbook in the text box, and many more. These methods are accessed by apply a driver aim and telephonedriver.methodName ().

Also Read:

Examples:

driver.get (“ https: //www.browserstack.com ”); driver.switchTo () .frame (1);

Browser Commands in Selenium

Browser commands provide control over the browser ’ s actions, which include open a browser, perform activeness on a web browsing, r and closing it.

These include launching a web page, fetching page title, page URL, rootage URL, shut the web page/ browser, etc.

1. Get command: This method loads a new web page in the live browser window, accepting a String as a argument and returning nullity.

Syntax:

get (String URL)

2. Get rubric command: This method fetches the rubric of the current web page, direct no argument, and returns a String.

Syntax:

getTitle ()

Example:

String title=driver.getTitle ();

3. Get current URL command: This command fetch the URL of the current web page, conduct no parameters, and returns a String.

Syntax:

getCurrentUrl ()

Example:

String URL=driver. getCurrentUrl ();

4. Get page source command: This command fetches the seed code of the current web page on the current web browser. It conduct no parameter and returns a String.

Syntax:

getPageSource ()

Example:

String pageSource=driver. getPageSource ();

5. Close dictation: This method closes the current web page that the driver is pointing to on the current browser window. If the current web page is the only browser window, it also terminates the browser window.

Syntax:

close()

Example:

driver.close ();

6. Quit command: This method terminates all the windows run by the WebDriver. It accepts no parameter and returns void.

Syntax:

quit()

Example:

driver. quit ();

Navigational Commands in Selenium

Navigational commands facilitate perform operation that involve navigating through web Page. They also expeditiously grapple a browser ’ s history and do actions like going back, forward, and review the current page.

1. Navigate To require: This method charge a new page in the existing browser window, takes a String as a parameter, and return void.

Syntax:

navigate () .to (String URL)

Example:

driver.navigate () .to (“ https: //www.browserstack.com ”);

2. Forward command: This method enable the web browser to click on the forward button in the subsist browser window. It accepts no parameter and returns vacancy.

Syntax:

navigate () .forward ()

Example:

driver.navigate () .forward ();

3. Back command: This method enable the web browser to chatter on the back button in the existing browser window. It accepts no parameter and returns void.

Syntax:

navigate () .back ()

Example:

driver.navigate () .back ();

4. Refresh command: This method refreshes the current web page in the existing web browser window. It consent no parameter and returns nothingness.

Syntax:

navigate () .refresh ()

Example:

driver.navigate () .refresh ();

WebElement Commands in Selenium

A web element is an HTML component that contains a start and end tag and the substance in between. Selenium cater a way to interact with all the web elements present on the web page with the help of web element commands.

Actions such as sending keys, snap on a push, reading text value, checking-unchecking radio buttons, selecting from a drop-down push, etc. can be performed with.

Some of the Webelement bidding are as follows:

1. Click command: This method do click operation on a web element.

Syntax:

click()

Example:

WebElement ele=driver.findElement (By.id (“ locator id ”)); ele.click ();

2. SendKeys command: This method enters text into the editable battlefield and guide a String as a argument.

Syntax:

sendKeys (String schoolbook)

Example:

WebElement ele=driver.findElement (By.id (“ textbox id ”)); ele.sendKeys (“ Hello ”);

3. Clear command: This command is used to clear the text area & # 8217; s content. It takes no argument and return void.

Syntax:

clear()

Example:

WebElement ele=driver.findElement (By.id (“ textbox id ”)); ele.sendKeys (“ Hello ”); ele.clear ();

4. Get text command: This method is used to regain the inner text of the web factor, which CSS do not hide. It occupy no parameter and returns a String.

Syntax:

getText()

Example:

WebElement ele=driver.findElement (By.id (“ textbox id ”)); String text= ele. getText ();

5. Get Attribute bid: This method retrieves the value of a specified attribute of a web factor. It takes a String as a parameter and returns a String.

Syntax:

getAttribute (String attribute)

Example:

WebElement ele=driver.findElement (By.id (“ textbox id ”)); String value = ele. getAttribute (“ id ”); // Returns the “ id ” attribute

6. Get Location bid: This method retrieves the location of a specific web element on the web page in terms of X and Y coordinates.

Syntax:

getLocation ()

Example:

WebElement ele=driver.findElement (By.id (“ textbox id ”)); Point point = ele. getLocation (); System.out.println (`` X cordinate: `` + point.x + `` Y cordinate: `` + point.y);

7. Get CSS Value command: This method fetches the CSS property value of the given web ingredient. It takes no parameter and returns a String value.

Syntax:

getCssValue ()

Example:

WebElement ele=driver.findElement (By.id (“ textbox id ”)); String cssValue= ele.getCssValue ();

8. Is Enabled command: This command is used to control if a particular web constituent is enable on the web page. It returns a boolean true value if the constituent is enabled or false if it is not enable.

Syntax:

isEnabled ()

Example:

WebElement ele=driver.findElement (By.id (“ textbox id ”)); boolean iris = ele. isEnabled ();

9. Is Selected command: This command is habituate to control if a especial web element, such as wireless or checkbo, x is select or not. It returns a boolean true value if the element is selected or false if it is not choose.

Syntax:

IsSelected ()

Example:

WebElement ele=driver.findElement (By.id (“ textbox id ”)); boolean flag = ele. IsSelected ();

10. Is Displayed command: This command is used to verify if a particular web ingredient is visible on the web page. It returns a boolean true value if the element is expose or false if it is not displayed.

Syntax:

isDisplayed ()

Example:

WebElement ele=driver.findElement (By.id (“ textbox id ”)); boolean flag = ele.isDisplayed ();

Radio Button and Check Box Commands

Radio button let a user select but one selection from a group, while checkboxes allow choose one or more pick. In Selenium, you can chatter these elements, deselect them when needed, and confirm if an option is selected. These commands make it easy to work with radio push and checkboxes in forms and test their behavior.

1. Click Command

This method is used to select a radio button or a checkbox.

Syntax:

click()

Example:

WebElement radioButton = driver.findElement (By.id (`` radioOption '')); radioButton.click ();

2. isSelected Command

This method verifies if a radio button or checkbox is selected. It returns a boolean value — true if selected and false if not.

Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script.

Syntax:

isSelected ()

Example:

WebElement radioButton = driver.findElement (By.id (`` radioOption '')); boolean isChecked = radioButton.isSelected ();

3. isEnabled Command

This method verifies if a radio push or checkbox is enable and can be interact with.

Syntax:

isEnabled ()

Example:

WebElement radioButton = driver.findElement (By.id (`` radioOption '')); boolean isEnabled = radioButton.isEnabled ();

Dropdown and Button Commands

Dropdowns let exploiter select an option from a list. They are used when a form has fixed pick, such as a nation, province, or category. Buttons execute an action when clicked and can be used to subject a variety, add an item to a cart, or displace to the next page.

Selenium furnish methods for selecting options from a dropdown menu and clicking on button. These bid help you mimic user behavior and confirm that descriptor, searches, and former interactional element work as expected.

1. Click Command (Button)

This method is expend to click a button element. It spark the activeness associated with the button, like submitting a form or opening a menu.

Syntax:

click()

Example:

WebElement submitButton = driver.findElement (By.id (`` submitBtn '')); submitButton.click ();

2. Quality Command (Dropdown)

The Select class in Selenium is apply to act with & lt; select & gt; elements. It provides methods to prefer alternative by visible text, value, or index.

Syntax:

Select select = new Select (driver.findElement (By.id (`` dropdownId ''))); select.selectByVisibleText (`` Option Text ''); select.selectByValue (`` optionValue ''); select.selectByIndex (2);

Example:

WebElement dropDown = driver.findElement (By.id (`` country '')); Select take = new Select (dropDown); // Select by visible text select.selectByVisibleText (`` Canada ''); // Select by value select.selectByValue (`` ca ''); // Select by index select.selectByIndex (3);

3. Get Selected Option Command (Dropdown)

This method retrovert the first selected option in the dropdown.

Syntax:

getFirstSelectedOption ()

Example:

WebElement dropDown = driver.findElement (By.id (`` commonwealth '')); Select select = new Select (dropDown); WebElement selectedOption = select.getFirstSelectedOption (); String optionText = selectedOption.getText ();

Window and Tab Handling Commands

Modern web applications often open links or results in new windows or tabs. In Selenium, you can switch between window and chit using built-in methods. These methods help manage multiple pages, making it easy to test scenarios like verifying a link in a new tab or elicit information from a pop‑up window.

1. Get Window Handle Command

This method returns the unique identifier of the current window.

Syntax:

getWindowHandle ()

Example:

String mainWindow = driver.getWindowHandle ();

2. Get Window Handles Command

This method returns the identifiers of all open window or tabloid.

Syntax:

getWindowHandles ()

Example:

Set & lt; String & gt; allWindows = driver.getWindowHandles ();

3. Switch To Window Command

This method switches the direction of the driver to a specific window or tab.

Syntax:

switchTo () .window (windowHandle)

Example:

for (String handle: allWindows) {driver.switchTo () .window (handle);}

4. Close Command

This method closes the current window.

Syntax:

close()

Example:

driver.close ();

Frame and iFrame Commands

Frames and iFrames engraft another HTML page within a page. In Selenium, you can exchange to a frame or iFrame before working with its elements. These commands help move in and out of frames, making locating and testing ingredient inside them easy.

1. Switch to Frame by Index

Switches the focusing to a frame or iFrame based on its position.

Syntax:

switchTo () .frame (power)

Example:

// Switch to the second frame on the page driver.switchTo () .frame (1); // Now you can locate elements within that frame WebElement form = driver.findElement (By.id (`` signupForm ''));

2. Switch to Frame by Name or ID

Switches the focus to a frame or iFrame use its name or ID attribute.

Syntax:

switchTo () .frame (`` frameName '');

Example:

// Switch to the frame with the id `` paymentFrame '' driver.switchTo () .frame (`` paymentFrame ''); // Enter card details driver.findElement (By.name (`` cardNumber '')) .sendKeys (`` 4111111111111111 '');

3. Switch to Frame by Web Element

Switches the focusing to a bod apply its WebElement.

Syntax:

switchTo () .frame (WebElement element)

Example:

WebElement frame = driver.findElement (By.id (`` iframe '')); driver.switchTo () .frame (frame);

4. Switch Back to Main Page

Switches the focus from a frame or iFrame back to the main page.

Syntax:

switchTo () .defaultContent ()

Example:

// Switch back to the main page after work in a frame driver.switchTo () .defaultContent (); // Now you can place factor in the main page WebElement card = driver.findElement (By.id (`` mainMenu '')); menu.click ();

Read More:

Actions Class Commands

The Actions class in Selenium is used for complex user interactions, such as mouse movements, drag and drop, right clink, and double clicks. These commands help assume advanced behaviour that goes beyond basic clicks and typing.

1. Mouse Hover Command

Moves the mouse arrow over a specific element.

Syntax:

moveToElement (WebElement element)

Example:

WebElement menu = driver.findElement (By.id (`` menu '')); Actions action = new Actions (driver); action.moveToElement (menu) .perform ();

2. Drag and Drop Command

Drags an element and drops it to a mark location.

Syntax:

dragAndDrop (WebElement seed, WebElement mark)

Example:

WebElement origin = driver.findElement (By.id (`` source '')); WebElement target = driver.findElement (By.id (`` target '')); new Actions (driver) .dragAndDrop (source, prey) .perform ();

3. Right Click Command

Performs a right click on an element.

Syntax:

contextClick (WebElement element)

Example:

WebElement ingredient = driver.findElement (By.id (`` factor '')); new Actions (driver) .contextClick (ingredient) .perform ();

4. Double-Click Command

Performs a threefold click on an element.

Syntax:

doubleClick (WebElement factor)

Example:

WebElement constituent = driver.findElement (By.id (`` ingredient '')); new Actions (driver) .doubleClick (element) .perform ();

Synchronization and Wait Commands

handgrip delays and ensure factor are available before interaction. These commands help you pen stable and reliable test scripts.

1. Implicit Wait Command

Adds a wait time for finding elements. The driver will canvas the page until the element is found or the time croak.

Syntax:

driver.manage () .timeouts () .implicitlyWait (Duration.ofSeconds (10));

Example:

// Set implicit hold to 10 seconds driver.manage () .timeouts () .implicitlyWait (Duration.ofSeconds (10)); // Now any element search will wait up to 10 seconds WebElement constituent = driver.findElement (By.id (`` elementId ''));

2. Explicit Wait Command

Waits for a specific condition, like an element being visible or clickable.

Syntax:

WebDriverWait wait = new WebDriverWait (driver, Duration.ofSeconds (10)); wait.until (ExpectedConditions.visibilityOfElementLocated (By.id (`` elementId '')));

Example:

WebDriverWait wait = new WebDriverWait (driver, Duration.ofSeconds (10)); // Wait until the login push is visible WebElement loginButton = wait.until (ExpectedConditions.visibilityOfElementLocated (By.id (`` loginButton ''))); // Click the button once it 's visible loginButton.click ();

3. Fluent Wait Command

Waits for an component with polling at regular interval and can ignore certain exception.

Syntax:

Wait & lt; WebDriver & gt; wait = new FluentWait & lt; & gt; (driver) .withTimeout (Duration.ofSeconds (30)) .pollingEvery (Duration.ofSeconds (5)) .ignoring (NoSuchElementException.class);

Example:

WebElement element = wait.until (driver - & gt; driver.findElement (By.id (`` elementId '')));

New Features and Commands in Selenium 4

Selenium 4 introduced new commands and improved characteristic that make exam automation more flexible. These include for finding elements establish on their position congenator to former components. This makes it easy to progress test playscript that are both reliable and readable.

1. Relative Locators

Selenium 4 added to locate element based on their position in copulation to early elements. This makes it uncomplicated to build robust locators when fixed identifiers aren ’ t available.

  • above() –Finds an constituent place above a specific element.

Syntax:

WebElement elem = driver.findElement (By.id (`` elementId '')); WebElement aboveElement = driver.findElement (withTagName (`` div '') .above (elem));
  • below()– Finds an factor located below a specific element.

Syntax:

WebElement elem = driver.findElement (By.id (`` elementId '')); WebElement belowElement = driver.findElement (withTagName (`` div '') .below (elem));
  • toLeftOf ()– Finds an element located to the left of a specific element.

Syntax:

WebElement elem = driver.findElement (By.id (`` elementId '')); WebElement leftElement = driver.findElement (withTagName (`` div '') .toLeftOf (elem));
  • toRightOf ()– Finds an element located to the rightfield of a specific constituent.

Syntax:

WebElement elem = driver.findElement (By.id (`` elementId '')); WebElement rightElement = driver.findElement (withTagName (`` div '') .toRightOf (elem));
  • near()– Finds an element located close to a specific constituent.

Syntax:

WebElement elem = driver.findElement (By.id (`` elementId '')); WebElement nearElement = driver.findElement (withTagName (`` div '') .near (elem));

2. New Window Command

Selenium 4 introduced a elementary method for opening and managing new windows or tabs.

Syntax:

driver.switchTo () .newWindow (WindowType.TAB); driver.switchTo () .newWindow (WindowType.WINDOW);

Example:

WebDriver newTab = driver.switchTo () .newWindow (WindowType.TAB); newTab.get (`` https: //www.browserstack.com '');

3. New Get Element Screenshot Command

Selenium 4 allows capturing a screenshot of a specific element, make it easy to formalise or document event.

Syntax:

element.getScreenshotAs (OutputType.FILE);

Example:

WebElement logo = driver.findElement (By.id (`` logo '')); File screenshot = logo.getScreenshotAs (OutputType.FILE);

How BrowserStack Supports Selenium Testing

BrowserStack is a testing platform that provides thousands of existent devices and browsers, advanced debugging tools, and unlined automation support. This allows you to run your Selenium scripts exactly as they would run in the hands of a existent user, spot issues betimes, and release better package.

Here are some key features of:

  • at Scale:Launch and scale Selenium test runs straightaway, from a handful of sessions to hundreds, without define up or managing any substructure.
  • No Local Setup Required:Instantly access browser and device environments without the need for local installation, driver, or virtual machine.
  • :Run test cases concurrently across browsers and program to trim performance clip and hasten up feedback.
  • :View screenshots, video recordings, console messages, and network logs to identify and fix tryout failure quickly.
  • for Locators:Maintain test stability even when elements modify, with AI‑driven recovery of broken or updated locators.
  • : Connect with Jenkins, GitHub Actions, CircleCI, and other CI tools to run automated Selenium tests as part of every body-build and deployment.

Talk to an Expert

Conclusion

Selenium require define how scripts locate elements, simulate user interactions, navigate between Page, and validate behavior across different browser. Understanding these commands yield testers precise control over the flow of machine-driven examination and countenance them to cover a wide range of scenario.

provide the environment where you can quiz these commands. It hosts an all-inclusive scope of devices, support parallel execution, and captures detailed logs, screenshots, and recordings for every test run.

Useful Resources for Selenium

Methods, Classes, and Commands

Configuration

XPath

Locators and Selectors

Waits in Selenium

Frameworks in Selenium

Miscellaneous

Best Practices, Tips and Tricks

Design Patterns in Selenium: Page Object Model and Page Factory

Action Class

TestNG and Selenium

JUnit and Selenium

Use Cases

Types of Testing with Selenium

Tags
96,000+ Views

# Ask-and-Contributeabout this issue 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