Selenium WebDriver Commands

What we have learned so far?

Part 1 – What is Selenium WebDriver? How to setup Selenium?


Part 2 – Locators in Selenium WebDriver – Id, Name, Class Name, Tag Name, CSS, XPath

https://www.onlyfullstack.com/locators-in-selenium-webdriver/

In Selenium WebDriver, we have an entirely different set of commands for performing different operations.

Types of Selenium WebDriver Commands

Selenium WebDriver Commands are divided into 3 types as below
1. Browser Commands in Selenium
2. Navigation Commands in Selenium
3. WebElement Commands in Selenium

Browser Commands in Selenium

The very basic browser operations of WebDriver include opening a browser; perform few tasks and then closing the browser.

Given are some of the most commonly used Browser commands for Selenium WebDriver.

1. Get Command

In WebDriver, this method loads a new web page in the existing browser window. It accepts String as parameter and returns void.

WebDriver webDriver = new ChromeDriver();
webDriver.get("https://onlyfullstack.blogspot.com");

2. Get Title Command

In WebDriver, this method fetches the title of the current web page.

String pageTitle = webDriver.getTitle();
System.out.println("Page Title - " + pageTitle);

3. Get Current URL Command

In WebDriver, this method fetches the string representing the Current URL of the current web page.

String currentUrl = webDriver.getCurrentUrl();
System.out.println("Current Url - " + currentUrl);

4. Get Page Source Command

In WebDriver, this method returns the source code of the current web page loaded on the current browser.

String pageSource = webDriver.getPageSource();
System.out.println("Page Source - " + pageSource);

5. Close Command

This method terminates the current browser window operating by WebDriver at the current time. If the current window is the only window operating by WebDriver, it terminates the browser as well.

webDriver.close();

6. Quit Command

This method terminates all windows operating by WebDriver. It terminates all tabs as well as the browser itself.

webDriver.quit();

Complete Program

public class BrowserCommands {
    public static void main(String[] args) {
        System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, "Documents/chrome-web-driver/chromedriver");
        WebDriver webDriver = new ChromeDriver();
        webDriver.get("https://onlyfullstack.blogspot.com");

        String pageTitle = webDriver.getTitle();
        System.out.println("Page Title = " + pageTitle);

        String currentUrl = webDriver.getCurrentUrl();
        System.out.println("Current Url = " + currentUrl);

        String pageSource = webDriver.getPageSource();
        System.out.println("Page Source = " + pageSource);

        webDriver.close();
    }
}

Output –

Page Title = Only Fullstack Developer
Current Url = https://onlyfullstack.blogspot.com/
Page Source = <html dir="ltr" xmlns="http://www.w3.org/1999/xhtml" xmlns:b="http://www.google.com/2005/gml/b" xmlns:data="http://www.google.com/2005/gml/data" xmlns:expr="http://www.google.com/2005/gml/expr"><head> ... rest of the html code

Navigation Commands in Selenium

WebDriver provides some basic Browser Navigation Commands that allows the browser to move backwards or forwards in the browser’s history.

1. Navigate To Command

This method loads a new web page in the existing browser window.

WebDriver webDriver = new ChromeDriver();
webDriver.navigate().to("https://onlyfullstack.blogspot.com");
System.out.println("Navigated to = "+webDriver.getCurrentUrl());

2. Forward Command

In WebDriver, this method enables the web browser to click on the forward button in the existing browser window.

webDriver.navigate().forward();
System.out.println("Forwarded to = "+webDriver.getCurrentUrl());

3. Back Command

In WebDriver, this method enables the web browser to click on the back button in the existing browser window.

webDriver.navigate().back();
System.out.println("Back to = "+webDriver.getCurrentUrl());

4. Refresh Command

In WebDriver, this method refresh/reloads the current web page in the existing browser window. It neither accepts anything nor returns anything.

webDriver.navigate().refresh();

Complete Program

public class NavigationCommands {
    public static void main(String[] args) throws InterruptedException {
        System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, "chrome-web-driver/chromedriver");
        WebDriver webDriver = new ChromeDriver();
        webDriver.navigate().to("https://onlyfullstack.blogspot.com");

        System.out.println("Navigated to = "+webDriver.getCurrentUrl());
        Thread.sleep(1000);

        webDriver.navigate().back();
        System.out.println("Back to = "+webDriver.getCurrentUrl());
        Thread.sleep(1000);

        webDriver.navigate().forward();
        System.out.println("Forwarded to = "+webDriver.getCurrentUrl());
        Thread.sleep(1000);

        webDriver.navigate().refresh();
        
        webDriver.close();
    }
}

Output

Navigated to = https://onlyfullstack.blogspot.com/
Back to = data:,
Forwarded to = https://onlyfullstack.blogspot.com/

WebElement Commands in Selenium

What is Web Element?

The term web element refers to a HTML element. The HTML documents are composed of HTML elements. It consists a start tag, an end tag and the content in between. For instance, a HTML element is written as: “<tagname> content </tagname>”.
As we have seen we can find the html elements with below method.

WebElement webElement = webDriver.findElement(element_to_be_search);

1. Send Keys Command

We use the sendKeys method to type the text into the text field.

WebElement emailTextField = webDriver.findElement(By.className("email-text-field-class"));
emailTextField.sendKeys("onlyfullstack@gmail.com");

2. Clear Command

We can use clear method to clear the text field.

WebElement emailTextField = webDriver.findElement(By.className("email-text-field-class"));
emailTextField.sendKeys("onlyfullstack@gmail.com");
Thread.sleep(1000);
emailTextField.clear();

3. GetText Command

We can use the getText() to get the text from the html element. In below example we will get the text of the link.

WebElement termsAndCondition = webDriver.findElement(By.xpath("//*[@id="post-body-39246966950473039"]/div/form/table/tbody/tr[1]/td[1]"));
        System.out.println("Name text (getText()) = " + termsAndCondition.getText());

4. Submit Command

we can submit the form with submit() method.

WebElement submitButton = driver.findElement(By.id("submitButton"));  
element.submit();

5. GetTagName Command

We can get the tag name(html element) of the WebElement.

emailTextField = webDriver.findElement(By.className("email-text-field-class"));
System.out.println("Tag Name (getTagName()) = " + emailTextField.getTagName());

6. Click Command

We can click on any html element with click() method.

WebElement radioButton = webDriver.findElement(By.id("male"));
System.out.println("Is Displayed? (isDisplayed()) = " + radioButton.isDisplayed());
System.out.println("Is Enabled? (isEnabled()) = " + radioButton.isEnabled());
System.out.println("Is Selected? (isSelected()) = " + radioButton.isSelected());

radioButton.click();
System.out.println("Is Selected after radio button click? (isSelected()) = " + radioButton.isSelected());

7. IsDisplayed, IsEnabled and IsSelected Command

isDisplayed() – used to check if the html element is displayed on the page or not.
isEnabled() – used to check if the html element is enable on the page or not.
isSelected() – used to check if the html element is selected or not.

WebElement radioButton = webDriver.findElement(By.id("male"));
System.out.println("Is Displayed? (isDisplayed()) = " + radioButton.isDisplayed());
System.out.println("Is Enabled? (isEnabled()) = " + radioButton.isEnabled());
System.out.println("Is Selected? (isSelected()) = " + radioButton.isSelected());

radioButton.click();
System.out.println("Is Selected after radio button click? (isSelected()) = " + radioButton.isSelected());

Output –

Is Displayed? (isDisplayed()) = true
Is Enabled? (isEnabled()) = true
Is Selected? (isSelected()) = false
Is Selected after radio button click? (isSelected()) = true
Lets go to our next tutorial where we will discuss below points :

Part 4 – How to Handle CheckBox and Radio Button in Selenium WebDriver

  – How to select Radio Button in Selenium WebDriver?
– How to select CheckBox in Selenium WebDriver?
– How to check if the Radio Button is selected in Selenium WebDriver?

– How to check if the CheckBox is selected in Selenium WebDriver?

Source Code
You can find the complete source code on below GitHub repository – 
https://github.com/onlyfullstack/selenium-tutorial

Selenium Tutorial
https://www.onlyfullstack.com/selenium-webdriver-tutorial/