Web Element Commands in Selenium πβ¨
Now that you've mastered browser commands, let's dive into the enchanting realm of Selenium commands designed to identify and interact with WebElements. These commands are your secret tools for handling text boxes, radio buttons, checkboxes, and more β the building blocks of automated test scripts! π
Text Box Commands - Unleashing the Power of Inputs π¬βοΈ
click()
- The Initial Encounter π±οΈ
Begin your journey by clicking on the textbox with the mighty click()
command.
driver.findElement(By.xpath("//div//input[@id='search']")).click();
sendKeys()
- Inscribing the Chronicles π
Enter the text into a text box by using the sendKeys()
command.
driver.findElement(By.xpath("//input[@id='id_q']")).sendKeys("fresher");
clear()
- Wiping the Slate Clean π§Ό
You can clear the value in a textbox by using the clear()
command.
driver.findElement(By.xpath("//input[@id='search']")).clear();
getLocation()
- Navigating Coordinates πΊοΈ
It is used to determine the relative position of an element on a web page. You can either use it to get the location of a particular element or to access the textbox area using the coordinates.
org.openqa.selenium.Point location;
location=driver.findElement(By.xpath("//input[@id='search']")).getLocation();
getSize()
- Measuring Dimensions ππ
If you need to retrieve the height and width (i.e.) the dimensions of an object, you can use the getSize()
command.
Dimension dimension = driver.findElement(By.id("GmailAddress")).getSize();
System.out.println("Height of webelement ---> " + dimension.height);
System.out.println("Width of webelement ---> " + dimension.width);
getAttribute()
- Unveiling Secrets π΅οΈββοΈ
You can view the value type in a search text box using the getAttribute()
command.
String fieldValue = driver.findElement(By.xpath("XPATHVALUE")).getAttribute("Attributevalue");
getText()
- Inner Chronicles π
If youβre looking to retrieve the inner text of a particular web element that is not hidden by CSS, use the getText()
command.
String elementText = driver.findElement(By.xpath("XPATHVALUE")).getText();
Radio Button/Check Box Commands - Embracing Choices π»βοΈ
isSelected()
- Verifying Choices βοΈ
Verify whether radio buttons or checkboxes have been selected with isSelected()
.
boolean isSelected = element.isSelected();
isDisplayed()
- In Plain Sight π
You can even verify if the web elements are visible or not using theisDisplayed()
command.
boolean isVisible = element.isDisplayed();
isEnabled()
- Ready for Action π
The isEnabled()
command can be used to check whether the web elements are enabled or not.
boolean isEnabled = element.isEnabled();
getTagName()
- Tagging the Elements π·οΈ
You can use the getTagName()
command to return the tag name of the web elements.
String tagName = element.getTagName();
DropDown Commands - Navigating the Choices πβ¬οΈ
In Selenium Java, dropdowns play a crucial role in web automation, offering various options for user interactions. Three common types of dropdowns are:
Static Dropdown:
Dynamic Dropdown:
Autosuggestive Dropdown:
For a more in-depth exploration of DropDown commands, check out our comprehensive guide in "Handling Dropdowns using Selenium Java". Click here to delve deeper into the specifics and gain additional insights. Happy reading!
Armed with these magical Web Element commands, you're now ready to weave intricate automation scripts with Selenium. Let the automation symphony begin! ππ€
Subscribe to my newsletter
Read articles from Rinaldo Badigar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Rinaldo Badigar
Rinaldo Badigar
Software Engineer with 3+ years of experience in Automation, with a strong foundation in Manual, API, and Performance testing, ensuring high-quality software delivery for Ecommerce and Banking applications.