Selenium Webdriver is one of the most widely used open-source website automation tools. There are various sets of commands available in Selenium Webdriver to perform different operations. In this article, we’ll try to discuss the most commonly used Selenium Webdriver commands along with their syntax for your better understanding.
If you want to become a Selenium Certified Specialist, then visit Mindmajix - A Global online training platform: “Selenium Certification Course”. This course will help you to achieve excellence in this domain.
There are various methods available in the Webdriver interface. Using the instance variable driver we can access those methods through driver.methodName(). To view the methods offered by Webdriver, just open the Eclipse IDE loaded with Selenium Webdriver jar files, create a driver object for WebDriver, and press the dot key.
To understand the syntax of the methods, consider an example
Method Name: First, we need to create an object of a class to access any method and then all public methods will be visible for the object.
Parameter: An argument that is passed to a method to perform a particular operation.
Return type: Methods can return a value or returning nothing (void). If the void is mentioned after the method, it means, the method is returning no value.
Let us first know the list of the basic Selenium Webdriver commands used in Selenium followed by a detailed explanation of each.
The commands offered by Selenium Webdriver are broadly classified into the following categories:
1. Browser Commands
2. Navigation Commands
3. WebElement Commands
Also Read: Ultimate Selenium RC Tutorial
The following are the most commonly used browser commands in selenium Webdriver:
This command loads a new URL in the current browser window. It accepts a String as a parameter and returns void. The command takes a single string type parameter that is usually a URL of the application under test.
For the Selenium IDE users, the command may look very much like the open command.
driver.get("https://google.com");
For loading a new web page, the respective web command can be written as
driver.get(URL);
// Or can also be written as
String URL = "URL";
driver.get(URL);
Example: The command to load the official website can be written as below:
driver.get("https://www.mindmajix.com")
This command is used to retrieve the title of the existing web page the user is working on. It accepts no parameter and returns a String. A null string is returned if the web page has no title.
driver.getTitle();
Or can also be written as
String Title = driver.getTitle();
This command fetches the URL of the current web page. It doesn’t require any parameter and returns a String value.
The respective command to retrieve the string denoting the current URL can be written as:
driver.getCurrentUrl();
//Or can be writen as
String CurrentUrl = driver.getCurrentUrl();
This command is used to return the source code of the web page loaded on the current browser. It requires no parameter and returns a string value.
To get the page source of the page, it is written as:
driver.getPageSource();
Or
String PageSource = driver.getPageSource();
The command can be used with various string operations like contains() to ascertain the presence of the specified string value
boolean result = driver.getPageSource().contains("String to find");
This command is used to terminate the current browser window at the current time. If the current window is the only window operating by WebDriver, it terminates the browser as well. It accepts no parameters and returns void.
It can be written as:
driver.close();
This command is used to terminate all windows operating by WebDriver. It terminates all tabs as well as the browser itself. It accepts nothing as a parameter and returns void.
The respective command is written as:
driver.quit();
------ Related Article: Selenium Commands With Examples ------
The navigation commands in WebDriver enable the browser to move forwards or backward in the browser’s history.
The following are the most commonly used Browser Navigation commands for Selenium WebDriver:
For loading a new web page in the current browser window, this command is used. The string is accepted as a parameter and returns void.
To navigate/load a new web page, this command is written as:
driver.navigate().to("www.mindmajix.com");
This command allows you to click on the back button in the current browser window. It neither accepts anything nor returns anything.
It takes you backward by one page on the browser’s history and written as:
driver.navigate().back();
This command lets the web browser to click on the forward button in the current browser window. It neither accepts anything nor returns anything.
It takes you forward by one page on the browser's history and can be written as:
driver.navigate().forward();
For refreshing/reloading the current web page in the existing browser window, this command is used. It neither accepts anything nor returns anything.
The respective command can be written as:
driver.navigate().refresh();
------ Related Article: How to use WebDriver Backed Selenium ------
Web element refers to an HTML element, which contains a start tag, an end tag and the content in between. We have various WebElement commands in Webdriver.
WebElement element = driver.findElement(By.id("UserName"));
UserName is the value of the id attribute that is used as a unique identification for the desired web element.The below mentioned are the most commonly used WebElement commands for Selenium WebDriver.
This command has no effect on other elements.
Command - element.clear();
Text entry elements are INPUT and TEXTAREA elements. It can be written as a
WebElement element = driver.findElement(By.id("UserName"));
element.clear();
//Or can be written as
driver.findElement(By.id("UserName")).clear();
Command: element.click();
This command helps you to interact with web elements like text elements, links, radio boxes and many more. It can be written as
WebElement element = driver.findElement(By.linkText("javaTpoint"));
element.click();
//Or can be written as
driver.findElement(By.linkText("javaTpoint")).click();
This command retrieves the visible innerText of the element. This accepts nothing as a parameter but returns a String value.
Command: element.getText();
It returns an innerText of the element, including sub-elements, without any leading or trailing whitespace. It is written as:
WebElement element = driver.findElement(By.xpath("anyLink"));
String linkText = element.getText();
This is used to determine whether or not the element is selected or not. It accepts nothing as a parameter but returns a boolean value(true/false).
Command: element.isSelected();
This operation only applies to input elements such as Select Options, Checkboxes, and Radio Buttons. Returns True if the element is currently selected or checked, otherwise false. It is written as
WebElement element = driver.findElement(By.id("Sex-Male"));
boolean status = element.isSelected();
//Or can be written as
boolean staus = driver.findElement(By.id("Sex-Male")).isSelected();
This command helps to determine if the current element is Enabled or not? It accepts nothing as a parameter but returns a boolean value(true/false).
Command: element.isEnabled();
It can be written as
WebElement element = driver.findElement(By.id("UserName"));
boolean status = element.isEnabled();
//Or can be written as
boolean staus = driver.findElement(By.id("UserName")).isEnabled();
This command used to determine whether the element is currently being displayed or not. It accepts nothing as a parameter but returns a boolean value(true/false).
Command: element.isDisplayed();
It is written as
WebElement element = driver.findElement(By.id("UserName"));
boolean status = element.isDisplayed();
//Or can be written as
boolean staus = driver.findElement(By.id("UserName")).isDisplayed();
This command is used for fetching the width and height of the rendered element. It accepts nothing as a parameter but returns the Dimension object.
Command: element.getSize();
It returns the element size on the page and is written as:
WebElement element = driver.findElement(By.id("SubmitButton"));
Dimension dimensions = element.getSize();
System.out.println("Height :" + dimensions.height + "Width : "+ dimensions.width);
This Command fetches the CSS property value of the given element. It accepts nothing as a parameter and returns a String value.
Command: element.getCssValue();
This command is used for locating the element location on the page. It accepts nothing as a parameter but returns the Point object.
Command: element.getLocation();
It is written as
WebElement element = driver.findElement(By.id("SubmitButton"));
Point point = element.getLocation();
System.out.println("X cordinate : " + point.x + "Y cordinate: " + point.y);
This command works well/better than the click() if the current element is a form or an element within a form. It accepts nothing as a parameter and returns nothing.
Command: element.submit();
If this causes the current page to change, then this method will wait until the new page is loaded. It is written as
WebElement element = driver.findElement(By.id("SubmitButton"));
element.submit();
//Or can be written as
driver.findElement(By.id("SubmitButton")).submit();
This command is used to get the value of the given attribute of the element. It accepts the String as a parameter and returns a String value.
Command: element.getAttribute();
It is written as
WebElement element = driver.findElement(By.id("SubmitButton"));
String attValue = element.getAttribute("id"); //This will return "SubmitButton"?
This command is used to get the tag name of the element. accepts nothing as a parameter and returns a String value.
Command: element.getTagName();
It can be written as
WebElement element = driver.findElement(By.id("SubmitButton"));
String tagName = element.getTagName();
//Or can be written as
String tagName = driver.findElement(By.id("SubmitButton")).getTagName();
This command simulates typing into an element and accepts CharSequence as a parameter and returns nothing.
Command: element.sendKeys("text");
It works fine with text entry elements like TEXTAREA and INPUT elements. The respective command is written as
WebElement element = driver.findElement(By.id("UserName"));
element.sendKeys("JavaTpoint"); ?
//Or can be written as
driver.findElement(By.id("UserName")).sendKeys("JavaTpoint");
Conclusion:
In this post, we tried our best to explain the various Selenium WebDriver commands. We hope they are helpful for you and let you work easily with Selenium.
Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:
Name | Dates | |
---|---|---|
Selenium Training | Nov 19 to Dec 04 | View Details |
Selenium Training | Nov 23 to Dec 08 | View Details |
Selenium Training | Nov 26 to Dec 11 | View Details |
Selenium Training | Nov 30 to Dec 15 | View Details |
Madhuri is a Senior Content Creator at MindMajix. She has written about a range of different topics on various technologies, which include, Splunk, Tensorflow, Selenium, and CEH. She spends most of her time researching on technology, and startups. Connect with her via LinkedIn and Twitter .