Important Selenium commands that every QA should master (in 10 minutes)

These are the commands 90% of the interviewers ask as an ice breaker of an interview.

Without these tips, just don’t think of giving an interview.

Navigational Commands

To navigate to a webpage URL, QA can use either of the following commands:

Using get method: driver.get (“https://www.google.com”);

Using the navigate method: driver.navigate().to(“https://www.google.com/photos”);

Forward Command

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

driver.navigate().forward();

Back Command

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

driver.navigate().back();

The Refresh Command

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

driver.navigate().refresh();

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

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.

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