WebElement functions

WebElement is an HTML element that helps the users to drive automation tests. Selenium WebDriver provides well-organized web page interactions through WebElements, such as locating elements, getting attribute properties, asserting text present in WebElement, and more. However, to interact with hidden elements in a web page, it is necessary to unhide the hidden elements first. Let's discuss Selenium WebElement functions further:

  • getText(): This function delivers the innerText attribute of WebElement. The following is the syntax for this function:
    driver.findElement(By.locatorType("path")).getText();

    The following is an example on the Google page that returns the innerText attribute of a Google search button using the getText() function:

    driver.get("https://www.google.com");
    System.out.println(driver.findElement(By.id("_eEe")).getText());

    JavaScriptExecutor is a Selenium interface to execute JavaScripts that returns the innerText attribute of a hidden element. It is important to unhide the hidden elements before extracting innerText, as shown in the following code snippet:

    WebElement Element = driver.findElement(By.locatorType("path"));
    JavascriptExecutor jse = (JavascriptExecutor)driver;
    System.out.println(jse.executeScript("return arguments[0].innerHTML", Element));
  • getAttribute(): This function delivers the value of a given attribute of an element. The following is the syntax for this function:
    driver.findElement(By.locatorType("path")).getAttribute("value");

    The values or properties of an attribute can be easily returned using the getAttribute() method. The attributes can be a class/id/name/value/or any other attribute. The following piece of code returns the class attribute value of a Google search button:

    driver.get("https://www.google.com");
    driver.findElement(By.xpath("//div[@id='lst-ib']")).getAttribute("class");
  • getTagName(): This function delivers the tag name of a given element. The following is the syntax for this function:
    driver.findElement(By.locatorType("path")).getTagName();

    Let's see a snippet to get tagName of a Google search text field element using the getTagName() function:

    driver.get("https://www.google.com");
    driver.findElement(By.xpath("//div[@class='sbib_b']")).getTagName();
  • isDisplayed():This function checks whether an element is displayed in a page or not. It returns a Boolean value (true or false). The following is the syntax for this function:
    driver.findElement(By.locatorType("path")).isDisplayed();

    This method confirms whether an element is visible in a page or not until the timeout occurs. The Google search text field element is asserted here to acknowledge that the page is opened properly or not. The following code snippet does the work for us:

    driver.get("https://www.google.com");
    WebElement Element = driver.findElement(By.name("q"));
    Assert.assertTrue(Element.isDisplayed());
  • isEnabled(): This function checks whether an element is enabled in a page or not. It returns a Boolean value (true or false). The following is the syntax for this function:
    driver.findElement(By.locatorType("path")).isEnabled();

    Let's see an example that checks the element's status using the isEnabled() method. In general, the user is not allowed to edit the text field when an element is disabled. However, if it is in the enabled status, the right action has to be performed, or an assertion failure can be thrown as follows:

    driver.get("https://www.google.com");
    WebElement Element = driver.findElement(By.name("q"));
    if(Element.isEnabled())
    {
      driver.findElement(By.name("q")).sendKeys("Selenium Essentials");
    }else{
      Assert.fail();
    }
  • isSelected(): This function verifies whether an element is selected or not. It returns a Boolean value (true or false). The following is the syntax for this function:
    driver.findElement(By.locatorType("path")).isSelected();

    In the following example, the if condition is used to confirm whether the combobox is selected or not:

    driver.get("http://www.angelfire.com/fl5/html-tutorial/ddmenu.htm");
    WebElement Element1 = driver.findElement(By.xpath("//select[@name='jump']/option[1]"));
    WebElement Element2 = driver.findElement(By.xpath("//select[@name='jump']/option[2]"));
    if(Element1.isSelected())
    {
      System.out.println("html tutorial is selected");
    }else if (Element2.isSelected()){
      System.out.println("altavista is selected");
    }
  • getSize(): This method returns the width and height (dimensions) of a rendered element, as follows:
    Dimension dimensions=driver.findElement(By.locatorType("path")).getSize();  
    dimensions.width;
    dimensions.height;

    Let's see how we can return the width and height of a Google logo in the Google search page:

    driver.get("https://www.google.com");
    System.out.println(driver.findElement(By.xpath("//div[@id='hplogo']")).getSize());
    Dimension dimensions=driver.findElement(By.xpath("//div[@id='hplogo']")).getSize();
    System.out.println("Logo Width : "+dimensions.width);
    System.out.println("Logo Height : "+dimensions.height);
  • getLocation(): This function returns the x and y coordinates with the point location of the top-left corner of an element.
    Point point = driver.findElement(By.locatorType("path")).getLocation();
    point.x;
    point.y;
    point.getX();
    point.getY();

    The getLocation() method returns the values as point objects. Let's see how to return the x and y coordinates of a Google logo in the Google search page. The following are the two methods to use the getLocation() method:

    • Method 1:
      driver.get("https://www.google.com");
      Point point = driver.findElement(By.xpath("//div[@id='hplogo']")).getLocation();  
      System.out.println("X Position : " + point.x);
      System.out.println("Y Position : " + point.y);
    • Method 2:
      driver.get("https://www.google.com");
      Point point = driver.findElement(By.xpath("//div[@id='hplogo']")).getLocation();
      System.out.println(point);
      System.out.println(point.getX() + "	" + point.getY());
  • getCssValue(): This method returns the value of any CSS properties. The following is the syntax for this function:
    driver.findElement(By.locatorType("path")).getCssValue("font-size");

    WebElement's style attribute values can be certainly attained using this method. Let's focus on the CSS properties of the Google logo, as follows:

    driver.get("https://www.google.com");
    WebElement element = driver.findElement(By.xpath("//div[@id='hplogo']"));
    System.out.println(element.getCssValue("font-size"));
    System.out.println(element.getCssValue("font-weight"));
    System.out.println(element.getCssValue("color"));
    System.out.println(element.getCssValue("background-size"));
..................Content has been hidden....................

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