Test on Real Devices

Give your users a seamless experience by testing on 3500+ real devices and browsers. Don't compromise with emulators and simulators

Get Started free
Home Guide Top Selenium Commands for Developers and Testers

Top Selenium Commands for Developers and Testers

By Jash Unadkat, Community Contributor -

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().

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(); 

BrowserStack Automate Banner

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.

Talk to an Expert

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
Automation Testing Selenium Selenium Webdriver

Featured Articles

5 Selenium tricks to make your life easier

Selenium Python Tutorial (with Example)

Automation Tests on Real Devices & Browsers

Seamlessly Run Automation Tests on 3500+ real Devices & Browsers