Introduction to Selenium Webdriver Commands
The Selenium Webdriver Commands are having several methods to access the Webdriver effectively. These methods are accessed using the variable of a driver in a simple format ” driver.methodName() “.
Top 6 Methods of Selenium Webdriver Commands
we have different methods of selenium webdriver commands as below:
1. get() Methods
We can use get() method in different ways as follows:
- get(): It will take a new browser and opens the URL in that browser. It will take a string type argument that is an URL of an application. below is the usage.
obj = driver()
- getclass(): It is used to get the class object, it will show the runtime of the object. below is the syntax.
- getcurrenturl(): It is used to get the URL of a webpage, which the user currently using. It does not require any parameter/argument in it and it will return a string. below is the syntax.
- getpagesource(): It is used to get the page source of a webpage. It does not require any parameter/argument and it will return a string. We can use it with different string operations on a specified string. below is the syntax.
result = obj.getpagesource().contains("string");
- gettitle(): It is used to extract the title of a webpage. It will return a null value if the webpage is not having a title. It does not require any parameter/argument and will return a string. below is the syntax.
title = obj.getTitle();
- gettext(): It is used to extract the text from the web element, particularly inner text. It does not require parameter/argument and it will return a string. It is commonly used commands to check the messages and labels. below is the syntax.
text = obj.findelement(by.id("text")).gettext();
- getattribute(): It is used to extract the value from an attribute. It requires a parameter/argument which refers to an attribute. below is the syntax.
- getwindowhandle(): It is used to handle more number of windows. When we are handling multiple windows we need this command. It is used to perform operations on a newly opened window. Without this, we can’t handle the newly opened window. We can also move to the old window when we want. This method is mainly focusing on handling multiple windows. below is the syntax.
sample = obj.getWindowHandle();
2. Locating Links by linkText() and partialLinkText()
It is used to find the links in the source. With this command, we can find the all links and go through them. This method can find the links and redirect them to them without our interaction.
3. Selecting Multiple Items in a Dropdown
We have two types of dropdowns in selenium as below.
- single select dropdown: it allows us to select only a single value at a time.
- multi-select dropdown: it allows us to select multiple values at a time.
select_By_Value = new Select(obj.findElement(By.id("SelectID_One")));
4. Submitting a Form
Most of the websites need authentication, i.e. submitting a form to go through. But with normal selenium commands or methods, we can not fulfill this requirement. If we want to fill a submission form with selenium we need to use the “ findElement “ method. To submit a form of any webpage first we need to find the elements to fill with some data. If the data, which we are providing to the form is valid and correct we can get the required data.
5. Handling Iframes
When we are automating a web application we need to have different frames in a window, to go back and forth between multiple frames. These iframes are used to insert another webpage into one webpage and also one document into another.
Consider the following source code.
<head><title>Software Testing Help - iframe session</title>
<input type="text" id="Username">UserID</input>
<input type="text" id="Password">Password</input>
<button id="LogIn">Log In</button>
The above HTML code represents the one iframe into another iframe. with this, we can access the child iframe, by navigating the parent iframe. To access child iframe first we need to go through the parent iframe and then we can access child iframe. After performing operations on child iframe we can go back to the parent iframe.
We can not access the child iframe by without accessing the parent iframe. If we want to work on child iframe first we need to use parent iframe.
- Selecting iframe by id:
- Locating iframe with tagName: when we are locating an iframe, we might face some issues if we are not maintaining standard properties. If we face this, the process will be complicated to locate the iframes and changing them. To overcome this situation we can locate the iframes with the “ tagName “ method.
the above command will locate the element with the tag name and will change it into an iframe. In the above example, get(0) is used to locate the iframe with the index value. The above syntax will give the program control to the parent frame.
- Locating iframe with the index:
ii) frame(Name of Frame):
6. close() and quit() Methods
There are two types of commands to close the web browser in selenium as below.
- close(): This method is used to close the window of the web browser, which is currently opened by the selenium. it doesn’t need parameter/argument to pass.
- quit(): This method is also used to close all the windows of the web browser, which are opened by the selenium. It doesn’t need a parameter/argument.
In this article, we used many commands and methods which are commonly using. selenium has many methods and commands to use the browser effectively. all these methods and commands are using the browser in the background. We can also make the browser invisible by making it headless. Many of these commands are used to extract the data from web pages and a few of them are used to submit the forms and some other operations.
All these commands and methods are extracting the data and submitting the data to the webpages is based on the source code of the webpage. Without having knowledge of source code and its elements we can not perform these operations on a webpage.
This is a guide to Selenium Webdriver Commands. Here we discuss the top 6 methods of Selenium Webdriver Commands with various command implementation. You may also have a look at the following articles to learn more –