25 – Selenium WebDriver API Commands
Selenium Class 25: Selenium WebDriver API Commands
Selenium WebDriver API Commands
i) Browser Regular Commands
ii) Browser Navigation Commands
iii) Commands on Web Elements
iv) Verification Commands on Web Elements
v) Others
i) Browser Regular Commands
1) get()
Description:
Opens a specified URL in the Browser window
Syntax:
driverObject.get(“URL”);
Example:
driver.get(“https://www.icicibank.com/”);
2) getTitle()
Description: Returns the Current Browser Title
Syntax:
String browserTitle = driver.getTitle();
Example:
driver.get(“https://www.icicibank.com/”);
Thread.sleep(3000);
String title = driver.getTitle();
System.out.println(title);
3) getCurrentUrl()
Description:
Returns current url of the Browser window
Syntax:
String browserUrl = driver.getCurrentUrl();
Example:
//Instantiate MS Edge Browser
System.setProperty(“webdriver.edge.driver”, “E:/MicrosoftWebDriver.exe”);
//Create MS Edge Browser Driver
WebDriver driver = new EdgeDriver();
//Launch gcrShop Admin Interface
driver.get(“http://www.gcrit.com/build3/admin/”);
//Click on “Online Catalog” Link in Admin Login Page
driver.findElement(By.linkText(“Online Catalog”)).click();
Thread.sleep(3000);
String url = driver.getCurrentUrl();
System.out.println(url);
ii) Browser navigation commands
4) navigate().to()
Description:
It loads a new page in the current Browser Window
Syntax:
driverObject.naviagte().to(“URL”);
Example:
//Instantiate Chrome Browser driver
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
//Create Google Chrome Driver
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(“https://www.google.com/”);
String browserUrl= driver.getCurrentUrl();
System.out.println(browserUrl);//Google Url
driver.navigate().to(“https://in.yahoo.com/”);
browserUrl= driver.getCurrentUrl();
System.out.println(browserUrl);//Yahoo Url
……………………………………….
Or
//Instantiate Chrome Browser driver
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
//Create Google Chrome Driver
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(“https://www.google.com/”);
System.out.println(driver.getCurrentUrl());//Google Url
driver.get(“https://in.yahoo.com/”);
System.out.println(driver.getCurrentUrl());//Yahoo Url
5) navigate().back()
Description:
It moves a single item back in the browser history
Syntax:
driverObject.navigate().back();
Example:
//Instantiate Chrome Browser driver
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
//Create Google Chrome Driver
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(“https://www.google.com/”);
System.out.println(driver.getCurrentUrl());//Google Url
driver.navigate().to(“https://in.yahoo.com/”);
String url =driver.getCurrentUrl();
System.out.println(url);//Yahoo Url
driver.navigate().back();
System.out.println(driver.getCurrentUrl());//Google Url
……………………………………….
Or
//Instantiate Chrome Browser driver
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
//Create Google Chrome Driver
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(“https://www.google.com/”);
System.out.println(driver.getCurrentUrl());//Google Url
driver.get(“https://in.yahoo.com/”);
String url =driver.getCurrentUrl();
System.out.println(url);//Yahoo Url
driver.get(“https://www.google.com/”);
System.out.println(driver.getCurrentUrl());//Google Url
6) naviagte().forward()
Description:
It moves a single item forword in the Browser history
Syntax:
driverObject.navigate().forword();
Example:
//Instantiate Chrome Browser driver
System.setProperty(“webdriver.chrome.driver”, “E:/chromedriver.exe”);
//Create Google Chrome Driver
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(“https://www.google.com/”);
System.out.println(driver.getCurrentUrl());//Google Url
driver.navigate().to(“https://in.yahoo.com/”);
String url =driver.getCurrentUrl();
System.out.println(url);//Yahoo Url
driver.navigate().back();
System.out.println(driver.getCurrentUrl());//Google Url
driver.navigate().forward();
System.out.println(driver.getCurrentUrl());//Yahoo Url
7) navigate().refresh()
Description:
It refreshes the Browser Window
Syntax:
driverObject.navigate().refresh();
Example:
//Instantiate MS Edge Browser driver
System.setProperty(“webdriver.edge.driver”, “E:/MicrosoftWebDriver.exe”);
//Create Edge Browser Driver for WebDriver interface
WebDriver driver= new EdgeDriver();
//Launch gcrShop Admin Home page
driver.get(“http://www.gcrit.com/build3/admin/”);
driver.findElement(By.name(“username”)).sendKeys(“abcdef”);
Thread.sleep(4000);
driver.navigate().refresh();
8) close()
Description:
It closes the focused Browser
Syntax:
driverObject.close();
Example:
//Instantiate MS Edge Browser driver
System.setProperty(“webdriver.edge.driver”, “E:/MicrosoftWebDriver.exe”);
//Create Edge Browser Driver for WebDriver interface
WebDriver driver= new EdgeDriver();
//Launch gcrShop Admin Home page
driver.get(“http://www.gcrit.com/build3/admin/”);
driver.close();
9) quit()
It closes all browsers that opened by WebDriver during execution
Syntax:
driverObject.quict();
Example:
//Instantiate MS Edge Browser driver
System.setProperty(“webdriver.edge.driver”, “E:/MicrosoftWebDriver.exe”);
//Create Edge Browser Driver for WebDriver interface
WebDriver driver= new EdgeDriver();
//Launch gcrShop Admin Home page
driver.get(“file:///E:/HTMLExamples/LoginPage.html”);
driver.findElement(By.linkText(“Sign In”)).click();
driver.quit();
iii) Commands on Elements
10) findElement
It finds first element within the current web page usin given locator
Syntax:
driverObject.findElement(By.locator(“value”).WebDriverCommand();
example:
//Instantiate MS Edge Browser driver
System.setProperty(“webdriver.edge.driver”, “E:/MicrosoftWebDriver.exe”);
//Create Edge Browser Driver for WebDriver interface
WebDriver driver= new EdgeDriver();
//Launch gcrShop Admin Home page
driver.get(“https://www.google.com/”);
driver.findElement(By.partialLinkText(“ma”)).click();
11) sendKeys()
It enters a value in to an Edit Box or Text box
Syntax:
driverObject.WebDriverCommand/findElement(By.elementLocator(“value”)).sendkeys(Parameter/”value”);
Example:
driver.findElement(By.name(“username”)).sendKeys(“india@123”);
or
String user=”India@123″;
driver.findElement(By.name(“username”)).sendKeys(user);
12) clear()
It clears the Value from an Edit box or Text Box
Syntax:
driverObject.WebDriverCommand/findElement(By.locatorname(“value”)).WebDriverCommand/clear();
Example:
driver.findElement(By.name(“username”)).sendKeys(“India123@gmail.com”);
Thread.sleep(4000);
driver.findElement(By.name(“username”)).clear();
13) click()
Description:
It clicks an Element (Button, Link, Radio Button, Checkbox)
Syntax:
driverObject.WebDriverCommand/findElement(By.ElementLocator(“LocatorValue”)).WebDriverCommand/click();
Example:
driver.findElement(By.linkText(“Gmail”)).click();
Thread.sleep(3000);
driver.findElement(By.linkText(“SIGN IN”)).click();
driver.findElement(By.cssSelector(“#identifierNext > content”)).click();
driver.navigate().to(“http://www.gcrit.com/build3/create_account.php?osCsid=bhqj38g35mq33rttsb4a70k077”);
Thread.sleep(3000);
driver.findElement(By.name(“gender”)).click();
Thread.sleep(3000);
driver.findElement(By.name(“newsletter”)).click();