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:
sendKeys()
isDisplayed()
isSelected()
submit()
isEnabled()
getLocation()
clear()
getAttribute()
getText()
getTagName()
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, returningtrue
orfalse
.
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, returningtrue
orfalse
.
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, returningtrue
orfalse
.
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. Clearing Text from an Input Field
Command: clear(): void
Usage: Clears the text from an input field.
Example:
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:
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:
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:
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:
button.click(); // Clicks on the button
Explanation of Code
button.click()
: Simulates a click action on the button, triggering any associated events.
0 Comments