In this Selenium WebDriver tutorial, I will show you the most commonly used Selenium WebDriver commands with simple examples so that you can learn them easily and use them effectively as per the need.
So here we go.
In order to perform any action on the browser or its elements like Button, Textbox, and Listbox, etc, the first thing that we have to do is to create the object of the Selenium Webdriver for the browser that you are using and the syntax to create the driver object is as follows.
For Chrome Browser:
WebDriver driver = new ChromeDriver();
For Firefox Browser:
WebDriver driver = new FirefoxDriver();
Selenium Webdriver Commands
We can categorize Selenium WebDriver commands in the following categories.
- Browser commands
- Navigation commands
- WebElement commands
- Action commands
Selenium WebDriver Browser Commands
Browser commands are those commands which can be performed on the browser itself to perform operation like opening a browser, closing a browser and getting page source and, etc. So here we go to see the Selenium WebDriver commands with examples.
1) get() – How to Open a URL in Selenium WebDriver?
The get() method is used to open a URL in the current browser.
Syntax:
driver.get(WebAppURL);
Example
1 2 3 4 5 6 |
//Navigate to the Google driver.get("https://www.google.com/"); //Or you can store the URL in a variable and then use that in the get method String URL = "https://www.google.com"; driver.get(URL); |
2) getCurrentUrl() – How to read the URL of the Webpage in Selenium WebDriver?
The getCurrentUrl() method returns the current URL opened in the browser. It accepts nothing as an input parameter but returns a String.
Syntax:
driver.getCurrentUrl();
Example
1 2 |
//Get current page URL String currentURL=driver.getCurrentUrl(); |
This method is usually used to check if you have navigated to the correct page. We can use an assert statement to check the actual vs expected URL.
Syntax:
Assert.assertEquals(expectedURL, driver.getCurrentUrl());
Example
1 2 |
String strURL = "https://www.google.com"; Assert.assertEquals(strURL, driver.getCurrentUrl()); |
3) getTitle() – How to get the title of the Webpage in Selenium WebDriver?
The getTitle() method returns the title of the current webpage. A null value is returned if the webpage has no title. It accepts nothing as an input parameter but returns a String.
Syntax:
driver.getTitle();
Example
1 2 |
//Get current page title String currentURL=driver.getTitle(); |
4) getPageSource() – How to read the page source of the WebPage in Selenium WebDriver?
The getPageSource() method is used to retrieve the source code of the current page. It accepts nothing as an input parameter but returns a String.
Syntax:
driver.getPageSource();
Example
1 2 3 4 5 |
//Get the entire Page Source Code String strPageSource = driver.getPageSource(); //Check whether a text exists in the page boolean flag = driver.getPageSource().contains("String to find"); |
5) close() – How to close the Browser in Selenium WebDriver?
This method close() method is used to close only the current window the WebDriver is currently controlling. It neither accepts nor returns anything.
Syntax:
driver.close();
Example
1 2 |
//close the current window open by Selenium WebDriver driver.close(); |
6) quit() – How to close all the Browser’s Window in Selenium WebDriver?
Unlike the close() method, the quit() method closes all windows opened by the WebDriver current session. It neither accepts nor returns anything.
Syntax:
driver.quit();
Example
1 2 |
//close all browser windows driver.quit(); |
Selenium WebDriver Navigation Commands
7) navigate() – How to Navigate to URL on the Existing WebPage Opened by Selenium WebDriver?
The navigate() method open a new web page in the current browser window opened by Selenium WebDriver. It accepts a String parameter and returns nothing.
Syntax:
driver.navigate().to(NewWebPageURL);
Example
1 2 |
//Load https://myskillpoint.com in the current browser window driver.navigate().to("https://myskillpoint.com"); |
8) forward() and back() – How to browse Forward and backward in Selenium WebDriver?
The forward() and backward() methods are used to move back and forth on an open window of Selenium WebDriver.The behavior of these methods is exactly as you are clicking the back and forward button of any web browser.
Syntax:
driver.forward();
Example
1 2 3 |
driver.navigate().to("https://myskillpoint.com"); driver.navigate().forward(); driver.navigate().back(); |
Selenium WebDriver WebElement commands
9) findElement() – How to find an element/object in Selenium WebDriver?
Every object on the webpage like Button, Textbox, ListBox, Radio buttons, Headings, WebTables and etc are WebElement for Selenium WebDriver. In short, any HTML element is a WebElement.
Before performing any action on any WebElement first of all we have to get that WebElement.The findElement can be used to get the required WebElement.
Example:
WebElement element = driver.findElement(By.name(“UserName”));
To know more about getting/locating a WebElement please refer to my post All about 8 Locators in Selenium Webdriver with Examples.
9) findElements() – How to find a list of the same type of Web elements in Selenium WebDriver?
This method returns a list of web elements matching with the same property.
Example:
List<WebElement> elements = driver.findElement(By.xpath(“//books”));
I will discuss this method in another post.
11) clear() – How to clear the value of a TextBox in Selenium WebDriver?
This method is used to clear the value present in the text box. It does not accept any parameter and returns nothing as the return type is void.
Syntax:
element.clear();
Example
1 2 3 4 5 6 7 |
// Store the textbox in a WebElement object WebElement element = driver.findElement(By.name("UserName")); //clear the value of textbox element.clear(); //or it can be written in one line as driver.findElement(By.name("UserName")).clear() |
12) click() – How to click on a Button/Link/WebEement in Selenium WebDriver?
This method is used to click on any WebElement. It does not accept any parameter and returns nothing as its return type is void.
Syntax:
element.click();
Example
1 |
driver.findElement(By.name("login")).click() |
13) submit() – How to Submit a form in Selenium WebDriver?
This method can be used to submit a form by pressing the button.
Syntax:
element.submit();
Example
1 |
driver.findElement(By.name("Button1")).submit() |
Note: If you see the type=Submit attribute in the buttons HTML Tag it’s better to use the submit method rather than as it works better.
For example, if you see the properties of the Google Search button you will see the following HTML tag attributes and you can easily notice it has an attribute “type=submit”
1 2 3 |
<input class="gNO89b" value="Google Search" aria-label="Google Search" name="btnK" type="submit" data-ved="0ahUKEwjakdCBjOTtAhX1gtgFHbFKDr0Q4dUDCAs"> |
If you perform a click operation on the Google Search button it probably won’t work and you might get the following exception message in your Eclipse Console.
Exception in thread “main” org.openqa.selenium.ElementNotInteractableException: element not interactable
However, if you use the submit method it will display the search result based on the provided keywords in the search textbox.
14) sendKeys() – How to set value in a TextBox in Selenium WebDriver?
This method can be used to set values in any text field element. It accepts String value as a parameter and returns nothing.
Syntax:
element.sendKeys(“textValue”);
Example
1 |
driver.findElement(By.name("username")).sendKeys("TestUser"); |
You can also set some value in a textbox and press the required keyboard key like sending the ENTER key together with it.
1 |
driver.findElement(By.name("password")).sendKeys("TestUser" + Keys.ENTER); |
15) getText() – How to get text value of an element in Selenium WebDriver?
This getText method returns the visible text of any web element. It accepts nothing as a parameter but returns a String value.
Syntax:
element.getText();
Example
1 2 3 4 |
String textBoxValue =driver.findElement(By.name("txtBox1")).getText(); //Or String linkTextValue =driver.findElement(By.id("link1")).getText(); |
16) isDisplayed() – How to check whether an element is being displayed in Selenium WebDriver?
This method checks whether an element is currently being displayed on the page. It accepts nothing as a parameter but returns a boolean value that is true or false.
Syntax:
element.isDisplayed();
Example
1 2 3 |
Boolean flag =driver.findElement(By.name("txtBox1")).isDisplayed(); |
Note: If the web element is present on the page but the property of the element is set to hidden in the DOM, in such case, it will return false value because the element is present in the DOM but not visible for the user.
17) isEnabled() – How to check whether an element is enabled in Selenium WebDriver?
This method lets you know whether an element is currently enabled or not on the page. If the element is enabled it returns true. It accepts nothing as an input parameter but returns a boolean value.
Syntax:
element.isEnabled();
Example
1 2 3 |
Boolean status =driver.findElement(By.name("txtBox1")).isEnabled(); |
18) isSelected() – How to check whether an element like CheckBox, Radio button is selected in Selenium WebDriver?
This method checks whether an element is currently selected or not. If the element is selected it returns true. It accepts nothing as an input parameter but returns a boolean value.
Syntax:
element.isSelected();
Example
1 2 3 |
Boolean status =driver.findElement(By.id("chkbox1")).isSelected(); |
19) getTagName() – How to get Tag Name of an element in Selenium WebDriver?
This method returns the tag name of the current element. It accepts nothing as a parameter and returns a String value.
Syntax:
element.getTagName();
Example
1 2 3 |
String tagName = driver.findElement(By.id("chkbox1")).getTagName(); |
20) getCssValue () – How to get the color and background color of a web element in Selenium WebDriver by getting cssValue?
This method is used to get the color of the web element and its background color. It accepts two types of parameters and returns an RGB color as a String value.
Color Parameter types:
- color
- background-color
Syntax:
element.getCssValue(colorParameter);
Example of getting element Color as well as Background color
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; public class GetCssColorValue{ public static void main(String[] args) throws InterruptedException { //Setting the path of Chrome Browser Driver String BrowserDriverPath= "C:\\SeleniumBrowserDrivers\\chromedriver.exe"; //Setting System property for Chrome browser Driver. System.setProperty("webdriver.chrome.driver",BrowserDriverPath); //Create a new Instance of Chrome Browser WebDriver driver = new ChromeDriver(); driver.get("https://www.facebook.com/"); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); String color=driver.findElement(By.name("login")).getCssValue("color"); String bgColor=driver.findElement(By.name("login")).getCssValue("background-color"); System.out.println("Color of login button: "+ color); System.out.println("Background color of login button: "+ bgColor); driver.close(); } } |
OutPut
Color of login button: rgba(255, 255, 255, 1)
Background color of login button: rgba(24, 119, 242, 1)
21) getAttribute() – How to get the desired attribute value of an element in Selenium WebDriver?
This method returns the attribute current element. It accepts String value as a parameter and returns a String.
Note: Attributes are properties of the element like id,name, class and etc
Syntax:
element.getAttribute(“id”);
Example: The following example will return the value of name attribute
1 2 3 |
String attributeName = driver.findElement(By.id("chkbox1")).getAttribute("name"); |
22) getSize() – How to get height and width of an element in Selenium WebDriver?
This getSize() method fetches the width and height of the web element. It accepts nothing as an input parameter but returns the Dimension object.
Syntax:
element.getSize();
Example: The following example shows how to get the height and width of an element.
1 2 3 4 5 |
Dimension size=driver.findElement(By.name("login")).getSize(); System.out.println("Login Button Height :" + dimensions.height + "Login Button Width :"+ dimensions.width); |
23) getLocation() – How to get the Location of an element in Selenium WebDriver?
This getLocation method locates the location of the element on the page. It accepts nothing as a parameter but returns the Point object.
Syntax:
element.getLocation();
Example: The following example shows how to get the X and Y Coordinate of an element.
1 2 3 4 5 |
Point point=driver.findElement(By.name("login")).getLocation(); System.out.println("X Coordinate : " + point.x + " Y Coordinate: " + point.y); |
Conclusion
We have learned various useful Selenium Webdriver commands.Hope now you could understand all these commands and you may be able to use them easily.
Recommended Posts
- Configure Selenium Webdriver With Eclipse
- What is Selenium WebDriver & its Architecture
- Selenium Webdriver Login Test Script
- How to Use CSS Selector in Selenium WebDriver | 13 Examples
- XPath in Selenium WebDriver with Example
- Java Installation & Environment Setup for Selenium WebDriver
- Get ToolTip Text in Selenium WebDriver
- Handle PopUps and Alerts in Selenium WebDriver
- What is Selenium WebDriver & its Architecture