Scientech Easy Selenium WebDriver Commands & Methods List | Scientech Easy

Thursday, December 13, 2018

Selenium WebDriver Commands & Methods List

In this tutorial, we will see very important and frequently used Selenium WebDriver commands or methods and their signatures in a simple way. To access selenium web driver methods, First, we need to create a driver object of the WebDriver and call the selenium web driver methods or commands using an object reference variable "driver" like this driver.methodName();
Selenium WebDriver commands can be classified as Get commands, Navigation commands, WebElement commands, Action commands, and Result commands.
Let's see first Get command in detail.

Get Commands

1. get(String arg) method 
➲ The get() method takes a String URL as a parameter and returns nothing.
➲ This method launches a new browser and opens the specified URL in the current browser window. The URL must be in the form of http://www.google.com. If the http is not included then it will throw a message "Cannot navigate to invalid URL".
Syntax: driver.get(http://www.google.com);
Selenium WebDriver Commands in Java, Selenium WebDriver methods, Selenium web driver browser commands.

For example:
    driver.get("http://www.google.com"); //Or It can be also written as String URL = "http://www.scientecheasy.com"; driver.get(URL);
 2. getCurrentURL() method 
➲ The getCurrentURL() method takes nothing as a parameter and returns the URL of the page currently loaded in the browser. 
➲ This method is used to get the String URL of the current web page loaded in the opened browser.
Syntax: driver.getCurrentURL();
For example:
    driver.getCurrentUrl(); //The above code can be also written as String CurrentUrl = driver.getCurrentUrl(); // Since the return type of getCurrentURL() method is String. So we will store it using variable currentUrl with String datatype.
3. getTitle() method 
➲ The getTitle() method takes nothing as a parameter and returns the page title of the currently loaded page. If the webpage has no title, it will return a null String.
➲ This method is used to get the title of the current web page.
Syntax: driver.getTitle();
For example:
    driver.getTitle(); // The above code can also be written as String title = driver.getTitle(); // Return type of getTitle is String.
4. getPageSource() method
➲ The getPageSource() method accepts nothing as a parameter and returns the source code of the current web page.
➲ This method is used to get the page source code of the currently loaded web page.
Syntax:  driver.getPageSource();For example:
    driver.getPageSource(); //Or String pageSource = driver.getPageSource(); // Since return type of the this method is String. Therefore, the output must be stored in a variable with datatype String.
5. getText()
➲ The getText() method accepts nothing as a parameter and returns a string value.
➲ This method is used to retrieve the inner text of the specified element.
Syntax: driver.findElement(By.locatorName("Specified Element")).getText();
For example:
    String text=driver.findElement(By.id("India")).getText(); System.out.println(text); // The above code can also be written as WebElement element=driver.findElement(By.id("India")); String text=element.getText(); // Return type of getText method is String. System.out.println("Inner Text: " +text);
6. getTagName() 
➲ The getTagName() method takes nothing as a parameter and returns a String value.
➲ This method is used to get the tagName of the WebElement returned by the findElement(By) method.
Syntax: element.getTagName(); 
For example:
    String tagName=driver.findElement(By.id("DownloadButton")).getTagName(); // or WebElement element=driver.findElement(By.id("DownloadButton")); String tagName=element.getTagName(); System.out.println(tagName);
7. getCssValue() 
➲ The getCssValue() method accepts nothing as a parameter and returns a String value.
➲ This method is used to fetch the value of the CSS property of the given web element when it is invoked.
Syntax: element.getCssValue(); 

8. getAttribute(String Name) 
➲ The getAttribute() method takes the String as a parameter and returns a String value.
➲ It is used to fetch or get the value of the attribute of the WebElement.
Syntax: element.getAttribute(); 
For example:
    WebElement element = driver.findElement(By.id("DownloadButton")); String attributeValue = element.getAttribute("id"); //This will return "DownloadButton" System.out.println(attributeValue);
9. getSize() 
 The getSize() method does not accept any parameter but returns the size of the element on the page. 
 This method is used to fetch the width and height of the rendered element.
Syntax: element.getSize(); 
For example:
    WebElement element = driver.findElement(By.id("DownloadButton")); Dimension elementSize = element.getSize(); System.out.println(“Height :” + elementSize.height + ”Width : "+ elementSize.width);
10. close() 
➲ The close method takes nothing as a parameter and returns nothing.
➲ This method is used to close only the browser window that web driver is currently controlling.
Syntax:  driver.close(); 
11. quit() 
➲ The quit() method accepts nothing as a parameter and returns nothing.
➲ This method is used to close all windows opened by WebDriver.
Syntax: driver.quit();

Navigation Commands

navigate() method
The navigate method is present in the web driver interface. It returns an object of type Navigation.
We can create a Navigation object by calling the navigate()method on the WebDriver object. 

There are four methods in the navigation class that is frequently used. They are as follows:
1. to()
➲ The to()method takes a String parameter and returns nothing.
➲ This method is used to navigate the URL of the web page in the current browser window. It will just navigate to the web page but not wait till the whole page gets loaded. 
Syntax: driver.navigate().to(URL);
For example:
    driver.navigate().to(http://www.scientecheasy.com); // Or String url="http://www.scientecheasy.com"; driver.navigate().to(url);
2. forward() 
➲ The forward() method neither accepts nor returns anything.
➲ This method is used to move forward by one page on the browser's history.
Syntax: driver.navigate().forward();

3. back() 
➲ The back() method neither accepts nor returns anything.
➲ This method is used to move back by one page on the browser's history.
Syntax: driver.navigate().back();

4. refresh() 
➲ The refresh() method neither accepts nor returns anything.
➲ This method is used to refresh the current web page.
➲ It performs the same function as pressing F5 in the browser. 
Syntax: driver.navigate().refresh();

WebElement Commands

What is WebElement?
The elements that are present on a webpage are called WebElement. It is an interface and represents HTML element. HTML documents consist of HTML element. An HTML element is made up of a start tag and end tag, with the content inserted in between: 
        <tagname> Content </tagname>where 
         <tagname> ➨ Start tag.
         </tagname> ➨ End tag.
The HTML element is everything from the start tag to the end tag:
        <p> My first paragraph </p>HTML element with no content is called an empty element. The empty element does not have an end tag such as <br> element which indicates a line break.

List of WebElement methods/actions

The following methods are the list of the WebElement interface. These methods are invoked through the web elements interface.
1. findElement(By) 
➲ It is an abstract method of the web driver interface.
➲ The findElement() takes a parameter of type By and returns a WebElement.
➲ This method is used to locate one web element in the current webpage using the locator provided in the parameter list. If the multiple elements are found with the same locator, it returns the first web element. 
➲ If it is unable to find an element using the locator provided in the parameter list then the findElement() method will throw a NoSuchElementException.
Syntax: driver.findElement(By.locatorName("Attribute value"));
For example:
    driver.findElement(By.id("Inida")); //Or WebElement country=driver.findElement(By.id("India")); // Return type is a WebElement.
2. findElements(By) 
➲ It is also an abstract method of web driver interface.
➲ The findElements() takes a parameter of type By and returns a list of WebElements.
➲ This method is used to find the list of web elements in the current webpage using the locator provided in the parameter list.
➲ If no elements are found using the given locator then the findElements() method returns an empty list.
Syntax: driver.findElements(By.locatorName("Attribute value")); 
For example:
    driver.findElements(By.xpath("//*")); // or List<WebElement> elements=driver.findElements(By.xpath("//*"));
3. sendKeys(CharSequence.....keysToSend) 
➲ sendKeys(CharSequence...) method accepts CharSequence as a parameter and returns nothing.
➲ This method is used to send a character sequence to an element when called on the findElement method.
Syntax: element.sendKeys("Text");
For example:
    driver.findElement(By.id("Username")).sendKeys("Scientech"); //or WebElement element=driver.findElement(By.id("username")); element.sendKeys("Scientech");
4. click() 
➲ The click() method accepts nothing as a parameter and returns nothing.
➲ This method is used to click on a WebElement such as a text element, link, radio button or any kind of other buttons.
Syntax: element.click(); 
For example:
    driver.findElement(By.linkText("Scientech")).click(); // or WebElement element=driver.findElement(By.linkText("Scientech")); element.click();
5. submit() 
➲ The submit() method accepts nothing as a parameter and returns nothing.
➲ This method is used when the current element is a form or an element within a form and the information will be submitted to the remote server.
➲ It will throw NoSuchElementException if the current element is not within a form.
Syntax: element.submit(); 
For example:
    ting driver.findElement(By.id("SubmitButton")).submit(); // or WebElement element=driver.findElement(By.id("SubmitButton")); element.submit();
6. isDisplayed() 
➲ The isDisplayed() method accepts nothing as a parameter and returns boolean value(true/false).
➲ This method is used to verify whether a certain element is displayed or not on the webpage.
➲ This method will return true if the element is present on the webpage. Otherwise, it will throw NoSuchElementFound exception if the element is not present on the page.
Syntax: element.isDisplayed(); 
For example:
    boolean status=driver.findElement(By.id(""Username)).isDisplayed(); // or WebElement element=driver.findElement(By.id("Username")); boolean status=element.isDisplayed(); System.out.println(status);
7. isEnabled() 
➲ The isEnabled method takes nothing as a parameter and returns boolean value(true/false).
➲ This method is used to verify whether a certain element is enabled or not in the webpage.
➲ It will return true if the element is enabled on the page.
Syntax: element.isEnabled(); 
For example:
    boolean status=driver.findElement(By.id("Username")).isEnabled(); // or WebElement element=driver.findElement(By.id("Username")); boolean status=element.isEnabled(); // Return type of isEnabled method is a boolean. // It can be used as WebElement element=driver.findElement(By.id("Username")); boolean status=element.isEnabled(); // Now check that the element is enabled or not. If it is enabled then enter the value. if(status){ element.sendKeys("Scientech"); // or WebElement element=driver.findElement(By.id("Username")); if(element.isEnabled){ element.sendKeys("Scientech"); } }
8. isSelected() 
➲ The isSelected() method takes nothing as a parameter and returns boolean(true/false).
➲ This method is used to verify whether a certain checkbox, radio button or option in the drop-down box is selected or not. It does not work with other elements.
➲ It returns true if the element is currently selected otherwise false.
Syntax: element.isSelected(); 
For example:
    // Suppose "India" is radio button. boolean status=driver.findElement(By.id("India")).isSelected; System.out.println(status); // or WebElement element=driver.findElement(By.id("India")); if(element.isSelected){ element.click(); }
9. clear() 
➲ The clear() method accepts nothing as a parameter and returns nothing.
➲ If the element is a text entry element, this method will clear the value.
Syntax: element.clear(); 
For example:
    WebElement element = driver.findElement(By.id("Password")); element.clear(); //Or driver.findElement(By.id("Password")).clear();

Difference between driver.get() and driver.navigate() method

driver.get() method 
1. driver.get() method is used to open the URL of the web application.
2. It will wait until the whole page gets loaded.
driver.navigate() method
1. This method is used for navigate the URL of the web page application.
2. It will just navigate to the page but not wait till the whole page gets loaded.

Difference between findElement() and findElements() method

findElement() method
1. The findElement() method is used to find one web element in the current webpage.
2. It returns WebElement object.
findElements() method
1. The findElements() method is used to find more than one web element in the current webpage.
2. It returns the list of web element object. 

Final words 
We hope that this article has covered all the important Selenium web driver commands and methods such as get commands, navigation commands, and web element commands with syntax and example in a simply way. We will discuss the action commands and result commands in further tutorial.