Software Testing - Interacting with Web Elements
Once the pieces have been located, Selenium offers ways to engage with them, such pressing buttons, typing text, or obtaining data.
click()
A mouse click on a web element is replicated by the click() function. It is frequently utilized for links and buttons.
Example:
driver.findElement(By.id("submit")).click();
sendKeys()
Using this method, text is typed into an input field. Text boxes and text areas frequently use it.
Example:
driver.findElement(By.id("username")).sendKeys("testUser");
clear()
A text input field's contents can be cleared using the clear() method.
Example:
driver.findElement(By.id("username")).clear();
getText()
The inner text of the designated element is retrieved using this function. It's helpful to confirm that the text displays as intended.
Example:
String message = driver.findElement(By.id("message")).getText();
getAttribute()
The value of a web element's given attribute can be obtained using the getAttribute() function.
Example:
String hrefValue = driver.findElement(By.id("link")).getAttribute("href");
isDisplayed()
The element's visibility on the page is verified using this procedure.
Example:
boolean isVisible = driver.findElement(By.id("login")).isDisplayed();
isEnabled()
The isEnabled() function determines if an element is interactable and enabled.
Example:
boolean isEnabled = driver.findElement(By.id("submit")).isEnabled();
isSelected()
This procedure determines if the element—such as a radio button or checkbox—is chosen.
Example:
boolean isSelected = driver.findElement(By.id("agree")).isSelected();