Top Selenium Commands for Developers and Testers
By Jash Unadkat, Community Contributor - December 17, 2024
Selenium is one of the most popular and widely used automation test suites to automate web applications. Selenium IDE, Selenium Grid, and Selenium WebDriver are the main components of Selenium.
Selenium WebDriver is the core component of the Selenium framework, which interacts with the web browser directly and supports multiple browsers such as Chrome, Firefox, Safari, Internet explorer, etc.
Selenium WebDriver offers predefined methods and functions enabling developers to interact directly with the browsers.
What are Selenium Commands?
Selenium commands refer to the predefined methods (such as getTitle(), getCurrentUrl(), click(), and more) provided by Selenium WebDriver that enables interaction with web elements and control browser behavior during test automation.
These commands help automate various browser tasks, such as navigating between tabs and windows, clicking on buttons or other elements, getting the current page title or URL, entering text in the text box, and many more. These methods are accessed by using a driver object and calling driver.methodName().
Also Read: Architecture of Selenium WebDriver
Examples:
driver.get(“https://www.browserstack.com”); driver.switchTo().frame(1);
Browser Commands in Selenium Commands
Browser commands provide control over the browser’s actions, which include opening a browser, performing actions on a web browse,r and closing it.
These include launching a web page, fetching page title, page URL, source URL, closing the web page/ browser, etc.
1. Get command: This method loads a new web page in the existing browser window, accepting a String as a parameter and returning void.
Syntax:
get(String URL)
2. Get title command: This method fetches the title of the current web page, takes no parameters, and returns a String.
Syntax:
getTitle()
Example:
String title=driver.getTitle();
3. Get current URL command: This command fetches the URL of the current web page, takes no parameters, and returns a String.
Syntax:
getCurrentUrl()
Example:
String URL=driver. getCurrentUrl();
4. Get page source command: This command fetches the source code of the current web page on the current web browser. It takes no parameter and returns a String.
Syntax:
getPageSource()
Example:
String pageSource=driver. getPageSource();
5. Close command: 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 operated by the WebDriver. It accepts no parameter and returns void.
Syntax:
quit()
Example:
driver. quit();
Navigational commands in Selenium Commands
Navigational commands help perform operations that involve navigating through web pages. They also efficiently manage a browser’s history and perform actions like going back, forward, and refreshing the current page.
1. Navigate To command: This method loads a new page in the existing browser window, takes a String as a parameter, and returns void.
Syntax:
navigate().to(String URL)
Example:
driver.navigate().to(“https://www.browserstack.com”);
2. Forward command: This method enables the web browser to click on the forward button in the existing browser window. It accepts no parameter and returns void.
Syntax:
navigate().forward()
Example:
driver.navigate().forward();
3. Back command: This method enables the web browser to click 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 accepts no parameter and returns void.
Syntax:
navigate().refresh()
Example:
driver.navigate().refresh();
WebElement commands in Selenium Commands
A web element is an HTML element that contains a start and end tag and the content in between. Selenium provides 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, clicking on a button, reading text values, checking-unchecking radio buttons, selecting from a drop-down button, etc. can be performed with Selenium’s web element commands.
Some of the Webelement commands are as follows:
1. Click command: This method performs 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 field and takes a String as a parameter.
Syntax:
sendKeys(String text)
Example:
WebElement ele=driver.findElement(By.id(“textbox id”)); ele.sendKeys(“Hello”);
3. Clear command: This command is used to clear the text area’s content. It takes no parameter and returns 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 retrieve the inner text of the web element, which CSS does not hide. It takes no parameter and returns a String.
Syntax:
getText()
Example:
WebElement ele=driver.findElement(By.id(“textbox id”)); String text= ele. getText();
5. Get Attribute command: This method retrieves the value of a specified attribute of a web element. 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 command: 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 element. 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 verify if a particular web element is enabled on the web page. It returns a boolean true value if the element is enabled or false if it is not enabled.
Syntax:
isEnabled()
Example:
WebElement ele=driver.findElement(By.id(“textbox id”)); boolean flag = ele. isEnabled();
9. Is Selected command: This command is used to verify if a particular web element, such as radio or checkbo,x is selected or not. It returns a boolean true value if the element is selected or false if it is not selected.
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 element is visible on the web page. It returns a boolean true value if the element is displayed or false if it is not displayed.
Syntax:
isDisplayed()
Example:
WebElement ele=driver.findElement(By.id(“textbox id”)); boolean flag = ele.isDisplayed();
Conclusion
Selenium is the first choice for all web automation testers due to its popularity, ease of learning, and integration with CI/CD tools. Every automation tester should know how to use Selenium commands correctly and efficiently in their automation scripts. These commands allow the testers to automate verifying that the web application works as expected.
Selenium can be used to automate tests across different browsers and devices. BrowserStack is the right platform for testers to accomplish this task flawlessly. With BrowserStack, you can get access to 3500+ real devices and browsers to test the applications.
Useful Resources for Selenium
Methods, Classes, and Commands
- Selenium Commands every Developer or Tester must know
- Selenium WebElement Commands
- Desired Capabilities in Selenium Webdriver
- Assert and Verify Methods in Selenium
- Understanding System setProperty in Selenium
- Select Class in Selenium : How to select a value in dropdown list?
- SendKeys in Selenium WebDriver
- getAttribute() method in Selenium: What, Why, and How to use
- How does Selenium isDisplayed() method work?
- findElement vs findElements in Selenium
- Types of Listeners in Selenium (with Code Examples)
- How to set Proxy in Firefox using Selenium WebDriver?
Configuration
- How to set up Selenium on Visual Studio
- How to configure Selenium in Eclipse
- Maven Dependency Management with Selenium
- How to Build and Execute Selenium Projects
XPath
- How to use XPath in Selenium?
- How to find element by XPath in Selenium with Example
- Top Chrome Extensions to find Xpath in Selenium
Locators and Selectors
- Locators in Selenium: A Detailed Guide
- CSS Selector in Selenium: Locate Elements with Examples
- How to Create Object Repository in Selenium
Waits in Selenium
- Wait Commands in Selenium C and C#
- Selenium Wait Commands: Implicit, Explicit, and Fluent Wait
- Understanding Selenium Timeouts
- Understanding ExpectedConditions in Selenium
- Understanding Role of Thread.sleep() in Selenium
Frameworks in Selenium
- Data Driven Framework in Selenium
- Implementing a Keyword Driven Framework for Selenium: A Practical Guide
- Hybrid Framework in Selenium
Miscellaneous
- How to create Selenium test cases
- How to set Proxy in Selenium?
- Difference between Selenium Standalone server and Selenium server
- Exception Handling in Selenium WebDriver
- How to use JavascriptExecutor in Selenium
- How to run your first Selenium test script
- Parallel Testing with Selenium
Best Practices, Tips and Tricks
- Top 5 Challenges Faced During Automation Selenium Testing
- 5 Selenium tricks to make your life easier
- 6 Things to avoid when writing Selenium Test Scripts
- Best Practices for Selenium Test Automation
- Why you should pay attention to flaky Selenium tests
- How to start with Selenium Debugging
- How to make your Selenium test cases run faster
- How to upgrade from Selenium 3 to Selenium 4
- Why you should move your testing to a Selenium Cloud?
Design Patterns in Selenium: Page Object Model and Page Factory
- Design Patterns in Selenium
- Page Object Model and Page Factory in Selenium
- Page Object Model and Page Factory in Selenium C#
- Page Object Model in Selenium and JavaScript
- Page Object Model and Page Factory in Selenium Python
Action Class
- How to handle Action class in Selenium
- How to perform Mouse Hover Action in Selenium
- Understanding Click Command in Selenium
- How to perform Double Click in Selenium?
- How to Drag and Drop in Selenium?
- How to Scroll Down or Up using Selenium Webdriver
- How To verify Tooltip Using Selenium
TestNG and Selenium
- Database Testing using Selenium and TestNG
- How to use DataProvider in Selenium and TestNG?
- All about TestNG Listeners in Selenium
- How to run parallel test cases in TestNG
- How to use TestNG Reporter Log in Selenium: Tutorial
- Prioritizing tests in TestNG with Selenium
JUnit and Selenium
- Understanding JUnit assertions for Selenium Testing with Examples
- How to run JUnit Parameterized Test in Selenium
- How to write JUnit test cases
- JUnit Testing Tutorial: JUnit in Java
- How to create JUnit Test Suite? (with Examples)
Use Cases
- Handling Login Popups in Selenium WebDriver and Java
- How to Launch Browser in Selenium
- How to handle Alerts and Popups in Selenium?
- How to get Selenium to wait for a page to load
- How to Find Element by Text in Selenium: Tutorial
- How to Read/Write Excel Data using Apache POI Selenium
- How to handle Captcha in Selenium
- How to handle multiple windows in Selenium?
- How to handle Multiple Tabs in Selenium
- How to find broken links in Selenium
- How to handle Cookies in Selenium WebDriver
- How to handle iFrame in Selenium
- How to handle Web Tables in Selenium
- How To Validate Text in PDF Files Using Selenium Automation
- Get Current URL in Selenium using Python: Tutorial
Types of Testing with Selenium
- Different Testing Levels supported by Selenium
- How to perform UI Testing with Selenium
- Regression Testing with Selenium: Tutorial
- UI Automation using Python and Selenium: Tutorial
- How to Run Visual Tests with Selenium: Tutorial
- How to perform ETL Automation using Selenium
- Cross Browser Testing in Selenium : Tutorial