Selenium Web Element is an object representation of HTML element on a Web page , such as button , text box or link which can be interacted with using Selenium WebDriver commands . Selenium WebElement provides methods for inspecting, locating and manipulating these elements for automating browser interactions .
Key Characteristics of Selenium Web Elements :
Locating Web Elements :
Before interacting with an web element , it must be located on the web page using locator strategy. Selenium provides findElement() method to find a single element and findElements() method to find the list of elements with the help of various locator types. Different locators are :
- Name : This locator locates elements using the name attribute.
- ID: This is the most reliable method as IDs are typically unique on a page
- Class Name : This locator locates the elements using the class attribute .
- Tag Name : This locator locates web elements by their HTML tag such as input or a .
- Link Text/Partial Link Text : This locator locates hyperlink elements with anchor tags < a > by their visible text .
- CSS Selector : This is a fast and powerful method to locate elements using CSS expressions .
- XPath : This locator is a flexible language for quering XML documents, which are useful for locating complex or dynamic elements when other locators fail .
Common Interactions Methods of Selenium Web Element :
- click() : This methos simulates a user click on an web element .(e.g., a button , link , or checkbox )
- sendKeys() : This method is used to type text into an editable element, like a text field or text area .
- clear() : This method clears the text from an input element .
- submit() : This submit method is used to submit a form.
- getText() : This method retrieves the visible inner text of the element.
- getAttribute(name) : This method retrieves the value of a specific HTML attribute( e.g. id , href , value )
- getTagName() : This method retrieves the HTML tag name of the elemenmt (e.g. “input ” )
- isDisplayed() : This method checks if the element is visible on the web page.
- isEnabled() : This method checks if the element is interactable or not.
- isSelected() : This method checks if an option (radio button , checkbox etc . ) is selected.
- getCssValue(propName): This method retrieves the value of a specific CSS property (e.g. “font-size” , “color” )
- getLocation() :This method retrieves the element’s top-left corner coordinates (x,y ) on the page.
- getSize() : This method retrieves the width and height of the element.
