Selenium WebDriver WebElement Commands: Interacting with Web Page Elements
Master essential Selenium WebDriver commands for interacting with WebElements (HTML elements) on a webpage. This guide provides a comprehensive list of common WebElement methods (click, sendKeys, clear, etc.), their syntax, and usage examples for effective web automation and testing.
Selenium WebDriver: WebElement Commands
Understanding WebElements
In Selenium, a WebElement
represents an HTML element on a webpage (e.g., a button, input field, link, etc.). To interact with elements, you first need to locate them using a suitable locator strategy (ID, name, XPath, CSS, etc.). Once located, you can use various methods to manipulate them.
Common WebElement Commands
Here are some frequently used methods for interacting with WebElements. Note that these are typically called on a WebElement
object obtained using a locator (like `driver.findElement(By.id("elementID"))`).
1. clear()
Clears the value of an input field.
WebElement element = driver.findElement(By.id("myTextField"));
element.clear();
2. sendKeys()
Types text into an input field.
WebElement element = driver.findElement(By.id("myTextField"));
element.sendKeys("This is some text");
3. click()
Clicks a button or link.
WebElement element = driver.findElement(By.id("myButton"));
element.click();
4. isDisplayed()
Checks if an element is visible on the page. Returns true
if visible, false
otherwise.
WebElement element = driver.findElement(By.id("myElement"));
boolean isVisible = element.isDisplayed();
5. isEnabled()
Checks if an element is enabled (interactive). Returns true
if enabled, false
otherwise.
WebElement element = driver.findElement(By.id("myInput"));
boolean isEnabled = element.isEnabled();
6. isSelected()
Checks if a checkbox or radio button is selected. Returns true
if selected, false
otherwise.
WebElement element = driver.findElement(By.id("myCheckbox"));
boolean isSelected = element.isSelected();
7. submit()
Submits a form.
WebElement element = driver.findElement(By.id("myForm")); // Find the form element
element.submit();
8. getText()
Gets the visible text of an element.
WebElement element = driver.findElement(By.id("myParagraph"));
String text = element.getText();
9. getTagName()
Gets the HTML tag name of an element.
WebElement element = driver.findElement(By.id("myElement"));
String tagName = element.getTagName();
10. getCssValue()
Gets the value of a CSS property for an element.
WebElement element = driver.findElement(By.id("myElement"));
String color = element.getCssValue("color");
11. getAttribute()
Gets the value of an attribute for an element.
WebElement element = driver.findElement(By.id("myInput"));
String value = element.getAttribute("value");
12. getSize()
Gets the dimensions (height and width) of an element.
WebElement element = driver.findElement(By.id("myElement"));
Dimension size = element.getSize();
System.out.println("Width: " + size.getWidth() + ", Height: " + size.getHeight());
13. getLocation()
Gets the location (x and y coordinates) of an element on the page.
WebElement element = driver.findElement(By.id("myElement"));
Point location = element.getLocation();
System.out.println("X: " + location.getX() + ", Y: " + location.getY());