Man must explore, and this is exploration at its greatest

Problems look mighty small from 150 miles up

Posted by Start Bootstrap on August 24, 2022

Information about web elements

Is Displayed

This method is used to check if the connected Element is displayed on a webpage. Returns a Boolean value, True if the connected element is displayed in the current browsing context else returns false.

This functionality is mentioned in, but not defined by the w3c specification due to the impossibility of covering all potential conditions. As such, Selenium cannot expect drivers to implement this functionality directly, and now relies on executing a large JavaScript function directly. This function makes many approximations about an element’s nature and relationship in the tree to return a value.

                          
// Navigate to the url
driver.get('https://www.google.com');

// Get boolean value for is element display
boolean isButtonVisible = driver.findElement(By.css("[name='login']")).isDisplayed();
                          
                        

Is Enabled

This method is used to check if the connected Element is enabled or disabled on a webpage. Returns a boolean value, True if the connected element is enabled in the current browsing context else returns false.

                          
//navigates to url
driver.get("https://www.google.com/");
      
//returns true if element is enabled else returns false
boolean value = driver.findElement(By.name("btnK")).isEnabled();    
                          
                        

Is Selected

This method determines if the referenced Element is Selected or not. This method is widely used on Check boxes, radio buttons, input elements, and option elements. Returns a boolean value, True if referenced element is selected in the current browsing context else returns false.

                            
//navigates to url
driver.get("https://the-internet.herokuapp.com/checkboxes");
         
//returns true if element is checked else returns false
boolean value = driver.findElement(By.cssSelector("input[type='checkbox']:first-of-type")).isSelected();       
                            
                          

Tag Name

It is used to fetch the TagName of the referenced Element which has the focus in the current browsing context.

                            
//navigates to url
driver.get("https://www.example.com");
         
//returns TagName of the element
String value = driver.findElement(By.cssSelector("h1")).getTagName();       
                            
                          

Size and Position

                            
It is used to fetch the dimensions and coordinates of the referenced element.
  
The fetched data body contain the following details:
  
X-axis position from the top-left corner of the element
y-axis position from the top-left corner of the element
Height of the element
Width of the element
                            
                          
                            
// Navigate to url
driver.get("https://www.example.com");

// Returns height, width, x and y coordinates referenced element
Rectangle res = driver.findElement(By.cssSelector("h1")).getRect();
                    
// Rectangle class provides getX,getY, getWidth, getHeight methods
System.out.println(res.getX());  
                            
                          

Get CSS Value

Retrieves the value of specified computed style property of an element in the current browsing context.

                            
 // Navigate to Url
 driver.get("https://www.example.com");
 
 // Retrieves the computed style property 'color' of linktext
 String cssValue = driver.findElement(By.linkText("More information...")).getCssValue("color");
                            
                          

Text Content

Retrieves the rendered text of the specified element.

                            
// Navigate to url
driver.get("https://example.com");

// Retrieves the text of the element
String text = driver.findElement(By.cssSelector("h1")).getText();
                            
                          


                            
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;

public class Rough {

    public static void main(String[] args) {
        
        WebDriverManager.chromedriver().setup();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.selenium.dev/documentation/webdriver/elements/information/");
        System.out.println(driver.findElement(By.id("size-and-position")).getClass());
        System.out.println(driver.findElement(By.id("size-and-position")).getTagName());
        System.out.println(driver.findElement(By.id("size-and-position")).getText());
        System.out.println(driver.findElement(By.id("size-and-position")).getLocation());
        System.out.println(driver.findElement(By.id("size-and-position")).getSize());
        driver.quit();
    }
}