Basic Selenium Commands


In this article, I will explain all basic selenium webdriver commands with syntax and example like get(), getTitle(), getCurrentUrl(), getWindowHandle(), Navigate(), Navigate().back(), Navigate().forward(), Navigate().refresh(), Close(), Quit(), switchTo(), findElement(By by), Click(), clear(), getText(), Sendkeys(), isDisplayed(), isEnabled(), isSelected(), select.

Selenium commands are called as selenence.
It is set of commands that are used to run your test scripts.

Some basic commands

  • Get(Url): It is used to open the provided web URL.
    Example: driver.get("https://www.irctc.co.in/eticketing/loginHome.jsf");

  • getTitle(): It is used to get the title of currently opened window.
    Example: String Title = driver.getTitle();
    Here, it will store title of window in Title variable which is of String type.

  • getCurrentUrl(): It is used to get the URL of currently opened window.
    Example: String CurrentURL = driver.getCurrentUrl();
    Here, it will store URL of window in CurrentURL variable which is of String type.

  • getWindowHandle(): It is used to get the handle of currently opened window.
    Example: String WindowHandle = driver.getWindowHandle();
    Here, it will store handle of window in WindowHandle variable which is of String type.

  • Navigate().to(Url): It is used to navigate to specified Url.
    Example: driver.navigate().to("http://www.easymobilerecharge.com/");

  • Navigate().back(): It is used to navigate to back page as per the history of instance.
    Example: driver.navigate().back();

  • Navigate().forward(): It is used to navigate to next page as per the history of instance.
    Example: driver.navigate().forward();

  • Navigate().refresh(): It is used to refresh the currently opened page.
    Example: driver.navigate().refresh();

  • Close(): Close is used to close the current open window. It will not close the browser if any other window is opened. If it's a last window opened in browser then it will quit browser too.
    Example: driver.close();

  • Quit(): This command will close all opened windows and will close the browser too.
    Example: driver.quit();

  • switchTo(): It is used to switch to another web element like alert, window or frame.
    Example:
    driver.switchTo().window(nameOfHandle);
    driver.switchTo().frame(nameOfFrame);

  • findElement(By by): It is used to find the element on web page using By class in bracket. There are many ways to find web element on web page like using ID, class, name, cssSelector, xpath, tag, linkText

  • Click(): It is used to click on specified element.
    Example: To click on 'Register' link of Mobile recharge website.
    driver.findElement(By.xpath("xpath of your web element")).click();

  • clear(): It is used to clear the content of specified web element.
    Example: driver.findElement(By.xpath("xpath of your web element")).clear();

  • getText(): It is used to get text of specified web element.
    Example: Suppose I have specified xpath of one link. So by using getText() method I will get the text of that link.
    String getText = driver.findElement(By.xpath("xpath of your web element")).getText();
    Here, it will store text of web element in getText variable which is of String type.

  • Sendkeys(): It is used to send explicit keys(like user pressing key from key board).
    Example: To write name in text box.
    driver.findElement(By.xpath("xpath of your web element")).sendKeys("ABCD");

  • isDisplayed(): It is used to verify that the specified web element is displaying or not on web page. If it is displaying then it will return result as a true otherwise false.
    Example: driver.findElement(By.xpath("xpath of your web element")).isDisplayed();

  • isEnabled(): It is used to verify that the specified web element is enabled or not. If it is enabled then it will return result as a true otherwise false.
    Example: If we have text box to enter name of user then and we want to verify that the text box is enabled or not.
    driver.findElement(By.xpath("xpath of your web element")).isEnabled();

  • isSelected(): It is used to verify that the specified web element like check box or radio button is selected or not. If it is selected then it will return result as a true otherwise false.
    Example: driver.findElement(By.xpath("xpath of your web element")).isSelected();

  • select: Select is a class used to list all the values from dropdown.
    Example: Suppose we want to select value from 'State' dropdown then below is the code.
    Select listbox = new Select(driver.findElement(By.xpath("xpath of dropdown")));
    listbox.selectByVisibleText("Maharashtra");


Complete program to understand all commands
package p;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class BasicProgram {
        public static void main(String[] args) throws InterruptedException {
                WebDriver driver = new FirefoxDriver();
                driver.get("http://www.easymobilerecharge.com/");

                String Title = driver.getTitle();
                System.out.println("Title of web page: " + Title);
                String CurrentURL = driver.getCurrentUrl();
                System.out.println("Url of web page: " + CurrentURL);
                String WindowHandle = driver.getWindowHandle();
                System.out.println("Handle of web page: " + WindowHandle);

                boolean verifyRegister = driver.findElement(By.linkText("Register")).isDisplayed();
                System.out.println("Is Register link displaying: " + verifyRegister);
                driver.findElement(By.linkText("Register")).click();

                boolean verifyEmail = driver.findElement(By.name("email")).isEnabled();
                System.out.println("Is Email text box enabled: " +verifyEmail);
                driver.findElement(By.name("email")).sendKeys("abc@gmail.com");

                boolean verifyCheck = driver.findElement(By.name("agree")).isSelected();
                System.out.println("Is checkbox selected: " + verifyCheck);

                String getText = driver.findElement(By.xpath("html/body/table[3]/tbody/tr/td[2]/table/tbody/tr[1]/td[2]/table/tbody/tr[1]/td/p")).getText();
                System.out.println("Text displayed on header of registration form: " + getText);

                driver.quit();
        }

}


Output:
Title of web page: Online Mobile Recharge | Online recharge service in India for Airtel Aircel Huch Vodafone Bsnl Tata Indicom and Reliance mobile
Url of web page: http://www.easymobilerecharge.com/
Handle of web page: {1c867d0a-2298-49b8-9828-5e39e2836402}
Is Register link displaying: true
Is Email text box enabled: true
Is checkbox selected: false
Text displayed on header of registration form: NEW REGISTRATION


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: