Important methods in Selenium

We will have a look at a few of the very important and frequently used methods and their signatures.

These are methods invoked by the WebDriver instance:

  • findElement(By) versus findElements(By): Both these methods take an argument of type By. The findElement method calls the FindElements method and returns a WebElement. If multiple elements are found with the same locator, it returns the first WebElement. The findElements, on the other hand, returns a list of WebElements. The findElement throws a NoSuchElementException if it is unable to find an element using the locator provided in the parameter list. The findElements returns an empty list if no elements are found using the given locator. 
  • get(String): The get method takes a String URL as a parameter. It navigates to the URL supplied as a parameter. The URL can be in the form of http://www.google.com. If the http:// is not included, then an Unhandled inspector error gets thrown with the message Cannot navigate to invalid URL.
  •  navigate(): The navigate method is present in the WebDriver interface. This method returns an object of type Navigation, which is a nested interface of WebDriver interface. We get the Navigation object by invoking the navigate() method on the WebDriver object. There are four methods in the Navigation class that are used frequently, namely: to(java.lang.String), forward(), back() and refresh(). The to(java.lang.String) method is used to load a particular webpage in the browser. The overloaded version of the to method is to(java.net.URL). This overloaded version makes it simple to pass a URL. Using back(), one can move back one page in the browser's history. Using forward(), one can move forward one page and refresh() refreshes the current page.

These are methods invoked at the WebElement level:

  • sendKeys(CharSequence... ): This method sends a character sequence to an element when invoked on the findElement method. The findElement method returns a WebeElement, after which we invoke sendKeys, since sendKeys is a method in the WebElement interface. It can also be invoked on findElements after fetching an individual element from the ArrayList of elements using the get(int) method.
  •  click(): This method is used to click on a WebElement—the like button, radio button, or any other kind of button. The prerequisite of using the click method is that the element should be visible and should have a height and width greater than 0.
  • submit(): If the current element is a form or an element within a form, the information will be submitted to the remote server. If the current element is not within a form, the NoSuchElementException is thrown.
  • selectByIndex(int): Selects the option at the index equal to the argument value.
  • getTagName(): Gets the tagName of the WebElement returned by the findElement(By) method.
  • getText(): Gets the visible text of the element, including subelements. 
  • getAttribute (String): Fetches the value (which is equal to the string parameter) of the attribute of the WebElement.
  • getCssValue(String): Fetches the value of the CSS property that is passed in when invoked on a WebElement.
  • getSize(): Fetches the width and height of the rendered element.
  • getLocation(): Returns a point that has the location of the top-left corner of the element.
  • isDisplayed(): Returns true if the element is displayed.
  • isSelected(): Returns true if this element is selected. This applies only to options in a Select object, checkboxes, and radio buttons. 
  • isEnabled(): Returns true if this element is enabled.

This method is invoked by the Select object:

  • selectByVisibleText(String): Used on a drop-down list. It selects the item from the list, the text of which matches the string argument.
..................Content has been hidden....................

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