Home Commands Tips To Use In Selenium

Commands Tips To Use In Selenium

Commands:- Commands are keys to drive test script and with commands can give actions for test script execution, means there is need to know about commands.

  1. Hit url


  1. Find elements

driver.findElement(By.id(“give id’s value as previously learned in post What is Locator in Selenium and How to Use?”);

For example we used “search related element”


Note: If using different locator then give value of other locator.

  1. Click element


  1. Send values for any text/input box

driver.findElement(By.id(“s”).sendKeys(“what is locator”));

  1. Select value from drop-down

Select category = new Select (driver.findElement(By.id(“.ui-datepicker-month”)));


  1. Select value by link text


  1. Hover Command

import org.openqa.selenium.interactions.Actions;  //need to add statement

import org.openqa.selenium.WebElement;  //need to add statement

Actions builder = new Actions(driver);

WebElement element = driver.findElement(By.linkText(“Put your text here”));


Alert Handling – Ok and Cancel Buttons

  1. Click on the ‘Cancel’ button of the alert.


  1. Click on the ‘OK’ button of the alert.


Page Scroll code:- Using Javascript

First way:-

import org.openqa.selenium.JavascriptExecutor;  (Import)

((JavascriptExecutor)driver).executeScript(“scroll(0,400)”);  (Write code)

Second way:-

import org.openqa.selenium.JavascriptExecutor;  (Import)

JavascriptExecutor js = ((JavascriptExecutor) driver);

js.executeScript(“window.scrollTo(0, document.body.scrollHeight)”);