Updated June 16, 2023
Introduction to Selenium Webdriver Commands
The Selenium Webdriver Commands have several methods to access the Webdriver effectively. These methods are accessed using the driver variable in a simple format of “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()
obj.get("https://duplichecker.com");
- getclass(): The class object is obtained using it; it will show the runtime of the object. below is the syntax.
obj.getclass();
- getcurrenturl(): It retrieves the URL of a webpage that the user is currently using. It does not require any parameter/argument in it, and it will return a string. below is the syntax.
obj.getcurrenturl();
- getpagesource(): It is used to get the page source of a webpage. It does not require any parameter/argument and 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 has no 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 the inner text. It does not require a parameter/argument and will return a string. The syntax below represents a commonly used command to check messages and labels.
text = obj.findelement(by.id("text")).gettext();
- getattribute(): It extracts the value from an attribute. It requires a parameter/argument which refers to an attribute. below is the syntax.
obj.findelement(by.id("findid")).getattribute("value");
- getwindowhandle(): It is used to handle more number of windows. When we are handling multiple windows, we need this command. 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();
obj.switchTo().window(sample);
2. Locating Links by linkText() and partialLinkText()
With this command, we can find all the links and go through them. This method can find the links and redirect them to them without our interaction.
Example:
obj.findElement(By.partialLinkText("go")).click();
obj.findElement(By.partialLinkText("abroad")).click();
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.
Example:
select_By_Value = new Select(obj.findElement(By.id("SelectID_One")));
select_By_Value.selectByValue("greenvalue");
select_By_Value.selectByVisibleText("Red");
select_By_Value.selectByIndex(2);
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 for any webpage, first, we need to find the elements to fill with some data. If the data we provide to the form is valid and correct, we can get the required data.
Example:
obj.findElement(By.<em>id</em>("username")).sendKeys("name");
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.
Example:
Consider the following source code.
<html>
<head><title>Software Testing Help - iframe session</title>
</head>
<body>
<div>
<iframe id="ParentFrame">
<iframe id="ChildFrame">
<input type="text" id="Username">UserID</input>
<input type="text" id="Password">Password</input>
</iframe>
<button id="LogIn">Log In</button>
</iframe>
</div>
</body>
</html>
The above HTML code represents one iframe into another iframe. With this, we can access the child iframe, by navigating the parent iframe. To access child iframe we first need to go through the parent iframe, and then we can access the child iframe. After performing operations on child iframe we can go back to the parent iframe.
We can not access the child iframe without accessing the parent iframe. If we want to work on child iframe, first, we need to use parent iframe.
- Selecting iframe by id:
obj.switchTo().frame("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 encounter this situation, locating and changing the iframes will become a complicated process. To overcome this situation, we can locate the iframes with the “ tagName “ method.
obj.switchTo().frame(obj.findElements(By.tagName("iframe").get(0));
the above command will locate the element with the tag name and change it into an iframe. The above syntax will give the program control over the parent frame.
- Locating iframe with the index:
i) frame(index):
obj.switchTo().frame(0);
ii) frame(Name of Frame):
obj.switchTo().frame("name");
iii) frame(element):
obj.switchTo().defaultContent();
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 currently opened web browser window by Selenium. It does not require any parameters or arguments to be passed.
- quit(): This technique is also used to close all Selenium-opened web browser windows. It doesn’t need a parameter/argument.
Example:
driver.close();
driver.quit();
Conclusion
In this article, we have utilized various commonly used commands and methods. Selenium offers various methods and commands that enable effective browser usage. These methods and commands operate on the browser in the background. Additionally, we can make the browser headless, thereby rendering it invisible. Users employ many of these commands to extract data from web pages, whereas they utilize others for form submission and other operations.
All these commands and methods extract the data and submit the data to the web pages 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.
Recommended Articles
We hope that this EDUCBA information on “Selenium Webdriver Commands” was beneficial to you. You can view EDUCBA’s recommended articles for more information.