WebElement Commands

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:

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:

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:

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:

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:

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:

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. Checking the Display Status of an Element

Command: isDisplayed(): boolean
Usage: Verifies if the element is visible on the page.

Example:

boolean isVisible = element.isDisplayed(); 
// Checks if the element is displayed

Explanation of Code

  • boolean isVisible: Declares a variable to store the visibility status of the element.
  • element.isDisplayed(): Returns true if the element is displayed, otherwise false.

8. Getting an Attribute Value of an Element

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

Example:

String value = inputField.getAttribute("placeholder"); 
// Retrieves the placeholder attribute value

Explanation of Code

  • String value: Declares a variable to store the attribute value.
  • getAttribute("placeholder"): Retrieves the “placeholder” attribute value of the input field.

8. Getting an Attribute Value of an Element

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

Example:

String value = inputField.getAttribute("placeholder"); 
// Retrieves the placeholder attribute value

Explanation of Code

  • String value: Declares a variable to store the attribute value.
  • getAttribute("placeholder"): Retrieves the “placeholder” attribute value of the input field.

9. Getting the Text of an Element

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

Example:

String text = label.getText(); 
// Retrieves the visible text of the label element

Explanation of Code

  • String text: Declares a variable to store the text content.
  • label.getText(): Retrieves the visible text of the label.

10. Getting the Tag Name of an Element

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

Example:

String tagName = element.getTagName(); 
// Retrieves the tag name of the element

Explanation of Code

  • String tagName: Declares a variable to store the tag name.
  • element.getTagName(): Retrieves the tag name of the element, such as “input” or “button”.

11. Clicking an Element

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

Example:

button.click(); 
// Clicks the button element

Explanation of Code

  • button.click(): Simulates a mouse click on the button element.
🤖
PrepCampusPlus AI Tutor
Scroll to Top