What are Selenium WebDriver Commands?
Selenium WebDriver commands are set of functions and method used for controlling or automating the web browser. these command helps developer and tester to write script programmatically using various languages (Java, C#, Python, etc.) to interact with web elements or perform various automation task. These set of command is core of the Selenium WebDriver, so it is very important to know these commands when you are learning about the Selenium.
These Commands are classified into three categories: –
1. Browser commands:
Browser commands provide exact control over the browser’s actions, like getting specific pages, extracting information such as page titles and URLs, accessing page source code, and controlling browser windows. Browser commands provide an easy way to interact with web applications and to perform automation and scraping data from web pages.
The Browser Command provides methods: get(), getTitle(), getCurrentUrl(), getPageSource() , close() and quit().
2. Navigation commands:
Navigation commands in Selenium WebDriver perform operations that involve navigating through web pages and controlling browser behaviour. These commands provide an efficient way to manage a browser’s history and perform actions like going back and forward between pages and refreshing the current page.
The Navigation Command provides four methods: to(), back(), forward(), and refresh(). These methods allow the WebDriver to perform the following operations:
3. WebElement commands:
To interact with various web element attributes such as (buttons, text fields, links, checkboxes, radio buttons, and more.) selenium webdriver provide a way to interact with all the webelement present on the web page and manipulate them by using Webelement commands. With help of these commands developer or tester can perform various tasks like typing on text field, clicking the button, reading text values, checking and unchecking radio buttons, selecting item from dropdowns etc.
The WebElement Commands provide method: sendKeys(), isDisplayed(), isSelected(), submit(), isEnabled(), getLocation(), clear(), getAttribute(), getText(), getTagName(), click() etc.
Some of the most commonly used commands:
Web Driver Commands |
Syntax and Description |
---|---|
1. get(String url) command |
Syntax: driver.get(URL);
It loads a new web page in the current browser window and accepts a string parameter that specifies the URL of the web page to be loaded. |
2. getTitle() Command |
Syntax: driver.getTitle();
It gets the title of the current web page displayed in the browser. It does not accept any parameters. It returns the title of the specified URL as a string. |
3. getCurrentUrl() Command
|
Syntax: driver.getCurrentUrl();
It gets the URL of the current web page shown in the browser. It does not accept any parameters and returns the URL as a string. |
4. getPageSource() Command |
Syntax: driver.getPageSource();
It gets the entire page source of the current web page loaded in the browser. It does not accept any parameters, but it does return the page source as a string. |
5. close() Command |
Syntax: driver.close();
It closes the current browser window or tab. Also, this command does not accept any types of parameters. It also does not return anything. |
6. quit() Command |
Syntax: driver.quit();
It closes all the browser windows and tabs for a particular WebDriver session. This command does not accept any parameters and not return anything. |
7. to() Command |
Syntax: driver.navigate().to(URL);
Loads a new web page in the current browser window. It accepts a string parameter that specifies the URL of the web page to be loaded. |
8. back() Command |
Syntax: driver.navigate().back();
Moves back one step in the browser’s history stack. It does not accept any parameters and does not return anything. |
9. forward() Command |
Syntax: driver.navigate().forward();
Moves forward one step in the browser’s history stack. It does not accept any parameters and does not return anything. |
10. refresh() Command |
Syntax: driver.navigate().refresh();
Reloads the current web page in the browser window. It does not accept any parameters and does not return anything. |
11. sendKeys() commands |
Syntax: // Create WebElement Entre text automatically into editable field while executing tests. these field are identified using locators like element id, name, class name, etc. |
12. isDisplayed() command |
Syntax: WebElement element = driver.findElement(By.id("gfg")); It verifies whether a web element is present and visible on the web page. Returns true if the element is displayed and false if not. |
13. isSelected() command |
Syntax: WebElement element = driver.findElement(By.id("software-testing")); It used on radio, checkboxes, dropdowns to check whether element is selected or not. If the specified element is selected, the value returned is true. If not, the value returned is false. |
14. submit() command |
Syntax: WebElement element = driver.findElement(By.id("SubmitButton")); It used to submit forms on browser. It doesn’t require a parameter and returns nothing. |
15. isEnabled() command |
Syntax: // Create WebElement Used to checks if an element is enabled for interaction on the web page or not. It returns true if the element is enabled and false if not. |
16. getLocation() command |
Syntax: WebElement element = driver.findElement(By.id("Selenium")); Retrieves the location of a specific web element on the page in terms of its X and Y coordinates. |
17. clear( ) command |
Syntax: WebElement ele = driver.findElement(By.id("edittext")); Used to Clears the content of text entry fields or text areas. It doesn’t require a parameter and returns nothing. |
18. getSize() command |
Syntax: WebElement element = driver.findElement(By.id("SubmitButton")); Used to retrieves the height and width of a rendered element on the web page. |
19. getAttribute() command |
Syntax: WebElement element = driver.findElement(By.id("Submit")); Used to retrieves the value of a specified attribute of a web element. |
20. click() command |
Syntax: // Create WebElement Used to perform click operation on a web element such as a button, link, or checkbox. |
Selenium WebDriver Commands Example:
Java
// Importing Libraries import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class WebDriverExampleGFG { public static void main(String[] args) { // Set the path to the ChromeDriver executable System.setProperty( "webdriver.chrome.driver" , "path_to_chromedriver.exe" ); // Initialize a new WebDriver instance (in this case, ChromeDriver) WebDriver driver = new ChromeDriver(); // get(String url) command driver.get( "https://www.example.com" ); // getTitle() Command String pageTitle = driver.getTitle(); System.out.println( "Page Title: " + pageTitle); // getCurrentUrl() Command String currentUrl = driver.getCurrentUrl(); System.out.println( "Current URL: " + currentUrl); // getPageSource() Command String pageSource = driver.getPageSource(); System.out.println( "Page Source: " + pageSource); // sendKeys() command WebElement textBox = driver.findElement(By.id( "TextBox" )); textBox.sendKeys( "w3wiki" ); // isDisplayed() command WebElement element = driver.findElement(By.id( "gfg" )); boolean isDisplayed = element.isDisplayed(); System.out.println( "Element is displayed: " + isDisplayed); // isSelected() command (for checkboxes, radio buttons, etc.) WebElement checkbox = driver.findElement(By.id( "software-testing" )); boolean isSelected = checkbox.isSelected(); System.out.println( "Checkbox is selected: " + isSelected); // submit() command WebElement submitButton = driver.findElement(By.id( "SubmitButton" )); submitButton.submit(); // isEnabled() command WebElement isEnabledElement = driver.findElement(By.id( "GFG" )); boolean isEnabled = isEnabledElement.isEnabled(); System.out.println( "Element is enabled: " + isEnabled); // getLocation() command WebElement locationElement = driver.findElement(By.id( "Selenium" )); int xCoordinate = locationElement.getLocation().getX(); int yCoordinate = locationElement.getLocation().getY(); System.out.println( "X Coordinate: " + xCoordinate); System.out.println( "Y Coordinate: " + yCoordinate); // clear() command WebElement clearElement = driver.findElement(By.id( "edittext" )); clearElement.clear(); // getSize() command WebElement sizeElement = driver.findElement(By.id( "SubmitButton" )); int elementHeight = sizeElement.getSize().getHeight(); int elementWidth = sizeElement.getSize().getWidth(); System.out.println( "Element Height: " + elementHeight); System.out.println( "Element Width: " + elementWidth); // getAttribute() command WebElement attributeElement = driver.findElement(By.id( "Submit" )); String attributeValue = attributeElement.getAttribute( "id" ); System.out.println( "Attribute Value: " + attributeValue); // click() command WebElement clickElement = driver.findElement(By.id( "w3wiki" )); clickElement.click(); // Close the current browser window/tab driver.close(); // Quit the WebDriver session driver.quit(); } } |
Selenium WebDriver Commands
Selenium WebDriver is a powerful tool for automating the web browser to perform certain tasks. Selenium supports multiple browsers (such as Chrome, Firefox, Edge, Safari, etc.) and multiple programming languages (such as Java, Python, C#, etc.) so, it is very easy to use and automate tasks on a browser. Selenium WebDriver provides predefined methods and functions that enable developers to interact directly with browsers.
By using WebDriver commands, you can automate various browser tasks such as navigating between browser tabs or windows, clicking on buttons or particular elements on web pages, performing actions on web elements, and other day-to-day tasks.
Let’s dive into more details about WebDriver commands in this article, we will learn about the Selenium WebDriver Commands in detail.