List of WebElement Commands
Now that we know what a WebElement Command is we will learn about some of the commands under it.
sendKeys() command
Sometimes we want some of the input fields to enter some content automatically while executing tests. Here, we can use the sendKeys() command to do the job after identifying the input web element using locating strategies like id, name, classname, etc.
Syntax
element.sendKeys("text");
The text here is supposed to be a CharSequence and this command works on text entry elements such as Input and Textarea.
Example
//Isolate the Web Element
WebElement inputElement = driver.findElement(By.id("username"));
//Use sendKeys to inject content
inputElement.sendKeys("User");
The above code can also be reduced to a single line
//Send value directly to the WebElement
driver.findElement(By.id("username")).sendKeys("User");
isDisplayed() command
When we need to verify whether a certain element is present and displayed we can use the isDisplayed() command. If the value is displayed it returns true, else the value returned is a NoSuchElementFound exception.
Syntax
element.isDisplayed();
Example
//Isolate the WebElement
WebElement element = driver.findElement(By.id("UserName"));
boolean status = element.isDisplayed();
//We can shorten the above code as well
boolean status = driver.findElement(By.id("UserName")).isDisplayed();
isSelected() command
When our webpage contains input elements such as radio buttons, checkboxes, select options, and menu items it is essential to determine whether the elements are selected or not. The isSelected command does exactly that. It returns true if a specified element is selected, else it returns false.
Syntax
element.isSelected();
Example
WebElement element = driver.findElement(By.id("Gender"));
boolean status = element.isSelected();
We can shorten the above code as well
boolean status = driver.findElement(By.id("Gender")).isSelected();
getLocation() command
This command returns the location of any specified web element on the page. This does not require any parameter and returns a Point object as a result. The x and y coordinate thus can be obtained respectively.
Syntax
element.getLocation();
Example
WebElement element = driver.findElement(By.id("UserName"));
Point location = element.getLocation();
System.out.println("X coordinate: " + point.x + "Y coordinate: " + point.y);
Frequently Asked Questions
1. What is the NoSuchElementFound exception?
Ans: When we try to access an iterable beyond its maximum limit, Java throws a NoSuchElementFound exception.
2. What is a point object?
Ans: Point objects contain an x coordinate and a y coordinate and are used to store locations in integer precision.
3. Is WebElement an interface or a class?
Ans: WebElement is an interface however all the abstract methods are implemented using the RemoteWebElement class.
4. How is WebDriver different from WebElement?
Ans: WebDriver class includes methods that work on Broad Actions that are not element specific on the Webpage. However, WebElement works on specific elements and has methods regarding the same.
Conclusion
In this blog, we discussed WebElement Commands and some of its most popular methods.
You may want to learn more about JUnit Annotations in Selenium here. We hope that this blog has helped you enhance your knowledge regarding render functions in the React testing library. Do upvote our blog to help other ninjas grow.
Learning never stops, and to feed your quest to learn and become more skilled, head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.!
Happy Learning!