WebElement Commands

WebElement commands are used to interact with various web element attributes such as buttons, text fields, links, checkboxes, and radio buttons. Selenium WebDriver provides a way to manipulate these web elements on a webpage. With these commands, developers or testers can perform actions like entering text in fields, clicking buttons, reading text values, and checking or unchecking checkboxes.

Common WebElement Commands include:

  1. sendKeys()
  2. isDisplayed()
  3. isSelected()
  4. submit()
  5. isEnabled()
  6. getLocation()
  7. clear()
  8. getAttribute()
  9. getText()
  10. getTagName()
  11. click()

1. Sending Keys to an Input Field

Command: sendKeys(CharSequence... keysToSend): void
Usage: Sends a sequence of keystrokes to the element.

Example:

Java
 WebElement inputField = driver.findElement(By.id("username")); inputField.sendKeys("testuser"); // Sends "testuser" to the input field 

Explanation of Code

  • WebElement inputField: Declares a variable to represent the input field on the webpage.
  • = driver.findElement(By.id("username")): Locates the input field by its ID.
  • inputField.sendKeys("testuser"): Sends the string "testuser" to the input field.

2. Checking if an Element is Displayed

Command: isDisplayed(): boolean
Usage: Checks whether the element is displayed on the page.

Example:

Java
 boolean isVisible = inputField.isDisplayed(); // Checks if the input field is displayed 

Explanation of Code

  • boolean isVisible: Declares a variable to store the visibility status of the input field.
  • = inputField.isDisplayed(): Checks if the input field is currently displayed on the page, returning true or false.

3. Checking if an Element is Selected

Command: isSelected(): boolean
Usage: Checks if a checkbox or radio button is selected.

Example:

Java
 boolean isSelected = checkbox.isSelected(); // Checks if the checkbox is selected 

Explanation of Code

  • boolean isSelected: Declares a variable to store the selection status of the checkbox.
  • = checkbox.isSelected(): Checks if the checkbox is currently selected, returning true or false.

4. Submitting a Form

Command: submit(): void
Usage: Submits the form to which the element belongs.

Example:

Java
 inputField.submit(); // Submits the form 

Explanation of Code

  • inputField.submit(): Submits the form associated with the input field, typically triggering a form submission.

5. Checking if an Element is Enabled

Command: isEnabled(): boolean
Usage: Checks whether the element is enabled for interaction.

Example:

Java
 boolean isEnabled = button.isEnabled(); // Checks if the button is enabled 

Explanation of Code

  • boolean isEnabled: Declares a variable to store the enabled status of the button.
  • = button.isEnabled(): Checks if the button is enabled for interaction, returning true or false.

6. Getting the Location of an Element

Command: getLocation(): Point
Usage: Gets the coordinates of the element on the page.

Example:

Java
 Point location = inputField.getLocation(); // Gets the location of the input field 

Explanation of Code

  • Point location: Declares a variable to store the coordinates of the input field.
  • = inputField.getLocation(): Gets the x and y coordinates of the input field on the webpage.

7. Clearing Text from an Input Field

Command: clear(): void
Usage: Clears the text from an input field.

Example:

Java
 inputField.clear(); // Clears the text from the input field 

Explanation of Code

  • inputField.clear(): Clears any text present in the input field, allowing for new input.

8. Getting an Attribute Value

Command: getAttribute(String name): String
Usage: Retrieves the value of a specified attribute of an element.

Example:

Java
 String className = inputField.getAttribute("class"); // Gets the class attribute of the input field 

Explanation of Code

  • String className: Declares a variable to store the class attribute value.
  • = inputField.getAttribute("class"): Retrieves the value of the "class" attribute from the input field.

9. Getting Text from an Element

Command: getText(): String
Usage: Retrieves the visible text of an element.

Example:

Java
 String visibleText = button.getText(); // Gets the visible text of the button 

Explanation of Code

  • String visibleText: Declares a variable to store the visible text.
  • = button.getText(): Retrieves the text displayed on the button.

10. Getting the Tag Name of an Element

Command: getTagName(): String
Usage: Gets the tag name of the element.

Example:

Java
 String tagName = inputField.getTagName(); // Gets the tag name of the input field 

Explanation of Code

  • String tagName: Declares a variable to store the tag name.
  • = inputField.getTagName(): Retrieves the tag name of the input field, such as "input" or "button".

11. Clicking an Element

Command: click(): void
Usage: Clicks on the element.

Example:

Java
 button.click(); // Clicks on the button 

Explanation of Code

  • button.click(): Simulates a click action on the button, triggering any associated events.