WebDriver Basic Syntax:

1. Creating New Instance of Firefox Driver:

WebDriver driver = new FirefoxDriver();

//Above given syntax will create new instance of Firefox driver.

2. Command to Open URL In Browser:


//This syntax will open specified URL in web browser.

3. Clicking on any element or button of webpage:


//Above given syntax will click on targeted element in WebDriver.

4. Store text of targeted element in variable:

String dropdown = driver.findElement(By.tagName("select")).getText();

//This syntax will retrieve text from targeted element and will store it in variable = dropdown

5. Typing text in text box or text area:

driver.findElement(By.name("fname")).sendKeys("My First Name");

//Above syntax will type specified text in targeted element.

6. Applying implicit wait in WebDriver:

driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

//This syntax will force WebDriver to wait for 15 second if element not found on page.

7. Applying Explicit wait in WebDriver with WebDriver canned conditions:

WebDriverWait wait = new WebDriverWait(driver, 15);

wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath("//div[@id='timeLeft']"), "Time left: 7 seconds"));

//Above 2 syntax will wait for till 15 seconds for expected text "Time left: 7 seconds" to be appear on targeted element.

8. Get page title in Selenium WebDriver:


//It will retrieve page title and you can store it in variable to use in next steps.

9. Get Current Page URL in Selenium WebDriver:


//It will retrieve current page URL and you can use it to compare with your expected URL

10. Get domain name using java script executor:

JavascriptExecutor javascript = (JavascriptExecutor) driver;

String CurrentURLUsingJS=(String)javascript.executeScript("return document.domain");

//Above syntax will retrieve your software application's domain name using WebDriver's java script executor interface and store it in to variable.

11. Selecting value from drop down in Selenium WebDriver:

Select mydrpdwn = new Select(driver.findElement(By.id("Carlist")));


//It will select value from drop down list using visible text value "Audi".

13. Navigate to URL or Back or Forward in Selenium WebDriver:




//1st command will navigate to specific URL, 2nd will navigate one step back and 3rd command will navigate one step forward.

14. Verify Element Present in Selenium WebDriver:

Boolean iselementpresent = driver.findElements(By.xpath("//input[@id='text2']")).size()!= 0;

//It will return true if element is present on page, else it will return false in variable iselementpresent.

15. Capturing entire page screenshot in Selenium WebDriver:

File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(screenshot, new File("D:\screenshot.jpg"));

//It will capture page screenshot and store it in your D: drive.

16. Generating Mouse Hover Event in WebDriver:

Actions actions = new Actions(driver);

WebElement moveonmenu = driver.findElement(By.xpath("//div[@id='menu1']/div"));



//Above example will move mouse on targeted element.

17. Handling Multiple Windows in Selenium WebDriver:

a) Get All Window Handles.

Set AllWindowHandles = driver.getWindowHandles();

b) Extract parent and child window handle from all window handles.

String window1 = (String) AllWindowHandles.toArray()[0] ;

String window2 = (String) AllWindowHandles.toArray()[1] ;

3) Use window handle to switch from one window to other window.


WebDriver example script for ->

TestCase : Open Kiprosh website and switch tabs.

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Test { 
public static void main(String aregs[])
String BaseUrl="http://www.kiprosh.com";
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
WebDriverWait wait=new WebDriverWait(driver,30);
driver.findElement(By.xpath("//a[contains(text(),'about')]")).click(); _//Open About Tab_
driver.findElement(By.xpath("//a[contains(text(),'Expertise')]")).click(); _//Open Expertise Tab_
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//h1[text()='Our Technology & Domain Expertise']")));