In this article we learn the what is webelement in selenium webdriver and also we learn the webelement methods in selenium webdriver.
WebElement In Selenium WebDriver
Webelement in selenium webdriver represents an HTML element. Generally, all interesting operations to do with interacting with a page will be performed through this webelement interface.
WebElement is an Interface.
Syntax of WebElement in Selenium WebDriver
FindElement command syntax:
WebElement eleName = driver. findElement (By(“…“));
FindElements command syntax:
List<WebElement> eleName = driver. findElements (By(“…”));
WebElement Methods in Selenium | Methods of WebElement In Selenium
If this element is a text entry element, this will clear the value.
2) click ()
Click this element.
3) findElement (By_)
Find the first WebElement using the given method.
4) findElements (By_)
Find all elements within the current context using the given mechanism.
5) getAttribute (java.lang.String name)
Get the value of a the given attribute of the element. Will return the current value, even if this has been modified after the page has been loaded.
6) getCssValue (java.lang.String propertyName)
Get the value of a given CSS property.
7) getSize ()
What is the width and height of the rendered element?
8) getText ()
Get the visible (i.e. not hidden by CSS) innerText of this element, including sub- elements, without any leading or trailing whitespace. Ruturn the innerText of this element.
9) isDisplayed ()
Is this element displayed or not? This method avoids the problem of having to parse an element’s “style” attribute.
10) isEnabled ()
Is the element currently enabled or not? This will generally return true for everything but disabled input elements.
11) isSelected ()
Determine whether this element is selected or not.
12) sendKeys (java. lang. CharSequence… keysToSend)
Use this method to simulate typing into an element, which may set its value.
13) submit ()
If this current element is a form, or an element within a form, then this will be submitted to the remote server.
Difference Between findElement and findElements
1) findElement () is used to find first web element on current page by provided locator mechanism such as id, xpath, css etc whereas findElements () is used to find all web elements on current page by provided locator mechanism such as id, xpath, css etc.
2) findElement () will return only first web element even provided locator locates more than one web element. findElements () returns all matching web elements. findElement () method internally calls findElements () method itself and return first indexed web element.
3) Return type of findElement () method is a WebElement whereas return type of findElements () method is a List<WebElement>.
4) findElement () will throw NoSuchElementException if no matching element is found. findElements () will return an empty list if no matching element is found. This is the reason findElements () is a better way for checking non-presence of web element.