Updated April 25, 2023
Introduction to Selenium Commands
Selenium commands are specifically used in the Selenium WebDriver framework for testing the application in a web browser. The commands can be categorized into four main groups, that are, Browser commands, WebElement commands, Get commands, and Navigation commands. Some commonly used commands in Selenium test scripts are sendKeys, click, IsDisplayed, IsSelected, refresh, navigate, back, quit, close, getTitle, getCurrentTitle, getSize, etc., for performing operations, for fetching values, for enabling actions on the web application.
- Selenium commands are used to automate web application testing and interact with the web page.
- There are three types of Selenium commands: actions, accessors, and assertions.
- Action commands include clicking on a button, typing into a field, or navigating to a URL.
- Accessor commands retrieve data from the web page, such as text, URL, or page title.
- Assertion commands verify the expected behavior of the web page, such as checking if a page element is visible or contains specific text.
- Common commands include get(url), click(), sendKeys(), get_attribute(name), etc.
Action Commands in Selenium
|click()||Clicks on a web element.|
|submit()||Submits a form.|
|send_keys()||Types the specified text into a text field or element.|
|clear()||Clears the content of a text field or element.|
|double_click()||Double-clicks on a web element.|
|context_click()||Right-click on a web element.|
|move_to_element()||Moves the mouse pointer to the center of a web element.|
|drag_and_drop()||Drags and drops a web element from one location to another.|
|key_down()||Simulates a keypress and holds it down.|
|key_up()||Releases a key that was previously held down.|
|click_and_hold()||Clicks and holds down a web element.|
|release()||Releases the mouse button that was previously held down.|
|perform()||Executes a series of actions that were defined by the Actions class.|
|pause()||Adds a pause or delay between actions.|
|scroll()||Scrolls the web page to a specified position.|
|select_by_visible_text()||Selects an option from a dropdown menu by its visible text.|
|select_by_value()||Selects an option from a dropdown menu by its value attribute.|
Basic Selenium Commands
Below are a few basic commands in Selenium.
1. Get method:- This method is a part of the driver class and can be accessed using a driver.get() method. It can navigate a web page by passing a URL as a parameter.
2. Navigate method:- This method, like ‘get’ is a part of the same class as the driver.navigate().to() method opens a web page like a driver.get() method.
3. Writing in a textbox:- The sendKeys() method help in writing in a textbox. It can also act for entering any element of text input type.
4. Refreshing the browser:- A browser can be refreshed in multiple ways. These are listed below:
- Using navigate().refresh() command
- Using sendKeys(Keys.F5) on any textbox on the webpage
- Using get(“URL”) with the current URL
- Using navigate().to(“URL”) with the current URL
5. Close Command:- This Selenium command helps kill the currently working window over which the WebDriver has control. The return type for this Command is void.
6. Get Title command:- This method helps return the current page’s title string in the browser. The return type for this method is a string. It is advisable to save this object as a string object or variable.
7. Get Current URL command:- This method is used when the string containing the URL currently opens in the browser in the window. It is helpful for logging purposes.
8. Get Page Source command:- This method helps get the source code of a web page. This returns a string value.
9. void forward():- This Selenium command helps in simulating the window to browse to the forward button action of a browser. It is supposed to move forward by a single page into the browser’s history data.
10. Click Command:- This method performs the click action by the web element. This is one of the most common methods of interacting with web elements like checkboxes, buttons, windows, etc.
The following commands should be known to you once you are comfortable with the basic Selenium commands.
- GetWindowHandle Command:- This Selenium command helps to get the window handle of the current window.
- GetWindowHandles Command:- This Command helps get window handles of all current windows that are open in a browser.
- SwitchTo Window Command:- This Webdriver command helps in moving between named windows by using the switchTo method. A user can also send a window handle to switchto().window() method, where it is also possible to iterate over any open window as well.
- SwitchTo Frame command:- By using this Command, it is possible to move between named frames by using the SwitchTo method.
- SwitchTo PopUp Command:- Similar to the above switchTo commands, this method can also help switch PopUp windows. Once this action is triggered, a pop-up is opened, and an alert can access it, returning the currently open alert object. With the help of this object, the user can accept, dismiss or read the contents into any prompt.
- IsDisplayed Command:- This method helps in checking whether an element is visible or not. It returns true if the element is present and visible on the page. It throws the necessary exception if the element is not present.
- IsEnabled:- This method returns either true or false based on the state of the element. It mostly returns true for all items except those intentionally disabled.
- Submit Command:- This method helps initiate the submission of an HTML form. It does not allow any parameter; if it leads to the current page, it will wait until the new page is loaded.
- GetCssValue Command:- This method provides the value of the CSS property which belongs to the element.
- GetLocation Command:- This method helps get the location of any element on the page. The user can quickly get the X and Y coordinates of any specified element.
Below are some advanced commands in Selenium:
1. Handling iframes by ID or Name:- To identify an iframe, the user must identify the ID or Name of the frame to be used. To do this, the user must first inspect the web page using web inspector tools. Once this is done following WebDriver command can be used to switch between different iframes.
2. Exception Handling:- There may be cases where programs get interrupted, and these interruptions can be said as exceptions. Like other languages, the Selenium WebDriver also supports exception handling. It can catch a single exception or even multiple exceptions.
3. Handling waits and conditions:- There are different commands which can handle waits. They can be named:
PageLoadTimeOut(time,unit) and implicitlyWait()
Similarly, for Conditions below can be used:
isDisplayed(), isEnabled(), isSelected()
Tips and Tricks to Use Selenium Commands
- The best method for creating a WebDriver Instance:- By using <Factory> design template user can create an object for a particular browser type.
- Method to check if an element exists:- The user can use <findElements> to check if an element exists or not.
- Avoiding exceptions and checking for an element together:- The above tag may lead to <NoSuchElementException>. This error can be avoided by using the wait feature, and it also allows testing the web element.
By making use of Selenium and these commands, any web application can be automated easily. Selenium is an open-source tool that is easily available and can be used for testing all web-based applications. The above commands can briefly help you get through the basics and advanced commands used in Selenium.
Frequently Asked Questions (FAQs)
Q1. What are the three types of Selenium commands?
Answer: The three types of Selenium commands are actions, accessors, and assertions.
- Actions: To perform actions such as clicking a button or typing into a field.
- Accessors: To retrieve data from the web page, such as text, URL, or page title.
- Assertions: To verify expected results, such as checking if a page element is visible or contains certain text.
Q2. What is the Command in Selenium?
Answer: A command in Selenium is a method that instructs the browser to perform a specific action or retrieve information from the web page.
Q3. How to use commands in Selenium?
Answer: To use commands in Selenium, you must first create a new instance of the Selenium WebDriver, navigate to a web page, and then use the appropriate command methods to interact with the page elements.
Q4. What are the five classes in Selenium?
Answer: The five classes in Selenium are WebDriver, WebElement, By, Actions, and Select.
- WebDriver: Controls the browser and executes commands.
- WebElement: Represents elements on the web page and provides methods to interact with them.
- By: Locates web elements by their attributes, such as ID, class, or name.
- Actions: Provides advanced user interactions such as drag-and-drop or mouse hovering.
- Select: Used to interact with dropdown menus and select options.
We hope that this EDUCBA information on “selenium commands” was beneficial to you. You can view EDUCBA’s recommended articles for more information,