App & Browser Testing Made Easy

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

Get Started free
Home Guide Selenium Commands every Developer or Tester must know

Selenium Commands every Developer or Tester must know

Jash Unadkat, Technical Content Writer at BrowserStack -

Selenium is indisputably one of the most preferred tools when it comes to test automation for any software. It is widely used for functional testing and cross browser testing of websites. It is very popular among the QAs because Selenium WebDriver is compatible with leading programming languages like Python, Java, Ruby to facilitate browser automation of popular browsers like Chrome, Firefox, Edge, etc. This means that QAs can quickly write Selenium test cases in their preferred language.

Besides being flexible in terms of programming language, Selenium framework also has firm community support from contributors across the world.

Being proficient in Selenium is a necessity for any QA who intends to run frequent and extensive automated tests. For this purpose, it is essential to master the specific basic Selenium commands and methods. These commands are simple functions or methods defined in several object-oriented programming languages.

This article aims at highlighting basic Selenium commands that are used to perform actions on a web browser.

Navigational Selenium Commands

Listed below are the basic Navigation commands in Selenium that one should be aware of.

1. Navigate To Commands
To navigate to a particular webpage URL, one can use either of the following commands:

Using get method

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

Using the navigate method

driver.navigate().to("https://www.browserstack.com/selenium");

2. Forward Command

The forward command navigates the browser forward by one page recorded in the browsing history.

driver.navigate().forward();

3. Back Command

The back command instructs the browser to redirect to the immediate previous webpage.

driver.navigate().back();

4. The Refresh Command

The Refresh command instructs the browser to reload or refresh the current web page.

driver.navigate().refresh();

Run your First Selenium Test for Free

Selenium Commands to search for specific Web Elements

For QAs to automate tests for a specific web page, it’s vital for scripts to identify the right elements on a web page to interact with. To identify specific elements on a web page, QAs can use the findElement() in Selenium command. It can be implemented with the following syntax:

WebDriver driver = driver.findElement (By.id(“text-box”));

The above method returns the specific web element that has text-box as a value of the id attribute. Additionally, there are multiple locators in Selenium that can be used to locate specific elements on a web page. Their function is similar to the “id locator” used in the code snippet above. These locators are as follows:

  • By.id
  • By.Name
  • By.CSS Selectors
  • By.classname
  • By.Link Text
  • By.PartialLinktext

Read More about locators in Selenium

Commands to handle frames in Selenium Webdriver

Frames in HTML can be used to divide a web-page vertically or horizontally. It is mainly used for displaying external content on a target web page, for example, an advertisement for any online programming course on a web page. To interact with any web element present within any frame, one needs to switch to that particular frame. This allows the user to identify elements present on that page and write tests accordingly.

QAs can switch between frames using the Switch.frame() function. The switch function can be implemented using three different locators: By.index, By.id, By WebElement. Refer to the commands below:

  • By Index
driver.switchTo().frame(1);

Switches to the frame with index number 1

  • By Id
driver.switchTo().frame(“resultframe”);

Switches the frame where the value of id attribute is resultframe

  • By Web Element
WebElement iframeElement = driver.findElement(By.id("resultframe"));
driver.switchTo().frame(iframeElement);

The WebElement command above identifies the web element and then passes it through the iframe element object.

Basic Get commands used in the Browser

Get commands or methods help QAs or developers to fetch specific parameters on the target webpage to be tested. Let’s quickly go through some basic Get commands in Selenium.

  • getCurrentUrl() – This command returns the URL of the currently active web page in the browser.
  • getPageSource() – This command helps in getting the entire HTML source code of the open web page.
  • getTitle() – This command can be used for displaying the title of the current web page.

Start your First Selenium Test for Free

Wait commands in Selenium

Wait commands in Selenium enable the QAs to pause the execution of test cases for a specified length time. These commands help in observing and troubleshooting errors that may arise due to time variations or any lags.

  • Implicit wait commands: These commands instruct the WebDriver to wait for a specified time before throwing an exception. Refer to the command below.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  • Explicit wait commands: These commands instruct the WebDriver to wait until a particular condition occurs before executing further scripts.
WebDriverWait wait = new WebDriverWait(driver,30);

Mastering Selenium is essential for any QA who wants to automate tests. Given that automated tests are absolutely necessary for thoroughly testing any website, knowing how to navigate and use Selenium is a non-negotiable skill for any tester worth their title. These basic selenium commands are an excellent place to start and provide a foundation for writing advanced tests with greater ease.

It is recommended to run Selenium Tests on real devices for more accurate test results. With real device cloud-based platform like BrowserStack, you get access to 3000+ real devices and browsers to run Selenium Tests with maximum test coverage. Using BrowserStack’s Cloud Selenium Grid you can run cross browser tests on multiple device-browser combinations simultaneously leveraging parallel testing.

Try BrowserStack for Free

Tags
Automation Testing Selenium Selenium Webdriver

Featured Articles

5 Selenium tricks to make your life easier

Selenium Python Tutorial (with Example)

App & Browser Testing Made Easy

Seamlessly test across 20,000+ real devices with BrowserStack