Overview of WebElement Functionality

The Selenium WebElement interface allows you to create variables of the WebElement type, which represent HTML elements on a web page. It also provides methods to interact with these elements.

Sometimes, elements on a web page can be dynamically modified, and before interacting with the element, all methods from the WebElement interface will check if the element's reference is still valid before performing any action on it. If the element's reference is not valid, the method will return a StaleElementReferenceException exception.


Updated information of the WebElement interface can always be found at https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/WebElement.html.

Here are the methods with their descriptions:

  • void clear(): This clears the value of a text field.
  • void click(): This performs a click on an element. It can only be used on visible elements with a width and height bigger than zero (0). If by clicking an element a new page is loaded, all previous references to the element will be invalid.
  • WebElement findElement(By by): This receives search parameters and returns the first matching element on the current page, or the NoSuchElementException if no element is found. We will develop this subject in detail in upcoming chapters.
  • java.util.List<WebElement> findElements(By by): This receives search parameters and returns all of the matching elements on the current page or an empty list if no element is found. We will develop this subject in detail in upcoming chapters.
  • java.lang.String getAttribute(java.lang.String name): Gets the current value of a given attribute of an element.
  • java.lang.String getCssValue(java.lang.String propertyName): This gets the value of a given CSS property.
  • Point getLocation(): This returns where on the page the top left-hand corner of an element is. Point is a set of (int x, int y) coordinates.
  • Rectangle getRect(): This returns the location and size of an element. Rectangle is a set of (int height, int width, int x, int y) measures and coordinates.
  • Dimension getSize(): This returns the size of an element. Dimension is a set of (int height, int width) measures.
  • java.lang.String getTagName(): This returns the tag name of an element. For the <div class="card-body">Sample text</div> element, this method will return div.
  • java.lang.String getText(): This returns the visible inner text of an element. If the element has subelements, it will return a string with no spaces.
  • boolean isDisplayed(): This indicates if an element is visible or not.
  • boolean isSelected(): This indicates if an element is selected or not. It applies to input elements such as checkboxes, options in a select button, or in a radio button.
  • boolean isEnabled(): This indicates if an element is enabled or not.
  • void sendKeys(java.lang.CharSequence… keysToSend): This simulates typing into an element.
  • void submit(): This submits the current form.

We now have a better understanding of the most frequently used methods of the WebElement class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.246.193