×
☰ See All Chapters

How to get data from web element in selenium WebDriver

Selenium WebDriver provide the below list of methods to read the web element data.

  1. getText 

  2. getAttribute 

  3. getCSSValue 

  4. getLocation 

  5. getSize 

  6. getTagName 

  7. getPageSource 

getText

getText() method returns the text content inside the tag.  We can apply "contains()","startsWith()", and "endsWith()" on the extracted text to perform any conditional check.

String text = driver.findElement(By.id("welcomeMessage")).getText();

assertTrue(text.contains("color"));

assertTrue(text.startsWith("Click on"));

assertTrue(text.endsWith("will change"));

getAttribute

getAttribute() method returns the value of attribute of the element.

String classAttribute = driver.findElement(By.id("date")).getAttribute("class");

getCSSValue

getCSSValue returns the value of a specified style attribute.

driver.findElement(By.id("date")).getCssValue("width");

getLocation

getLocation() method is used to get the relative position of an element relative to the top-left corner of the web page of which the (x, y) coordinates are assumed as (0, 0). This method returns the position in the form of Point object.

getX() and getY() methods of Point object then returns the position. This method can be of used if a test script tries to validate the layout of a web page and method can be used with all WebElements.

Point point = driver.findElement(By.id("yourname")).getLocation();

point.getX();

point.getY();               

getSize

getSize() method returns the width and height of the WebElement in the form of Dimension object. getWidth() and getHeight() methods of Dimension object are then used to get the width and height. This method can be used on all WebElements.

Dimension dimension = driver.findElement(By.id("yourname")).getSize();

dimension.getWidth();

dimension.getHeight();

getTagName

getTagName() method is used to get the tag name of the WebElement. This method can be used on all WebElements.

driver.findElement(By.id("welcomeMessage")).getTagName();

getPageSource

getPageSource() method is used to get the complete page text source. So, this method returns the complete html code that is rendering the page.

System.out.println(pageSource);

You can write the script and test these using our Test Page

elements-information-in-webdriver-0
 

import org.openqa.selenium.By;

import org.openqa.selenium.Dimension;

import org.openqa.selenium.Point;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

 

public class Example {

 

        public static void main(String[] args) {

 

                // configure chromedriver

                System.setProperty("webdriver.chrome.driver", "F:\\My_Programs\\Selenium\\ChromeDriver\\chromedriver.exe");

 

                WebDriver driver = new ChromeDriver();

               

                // Launch website

                driver.get("https://www.tools4testing.com/contents/selenium/testpages/12-get-elements-information-in-webdriver-testpage");

               

                String text = driver.findElement(By.id("welcomeMessage")).getText();

                System.out.println("Text value: " + text);

               

                String tagName = driver.findElement(By.id("welcomeMessage")).getTagName();

                System.out.println("Tag Name: " + tagName);

               

                String width = driver.findElement(By.id("date")).getCssValue("width");

                System.out.println("width: " +width);

               

                String classAttribute = driver.findElement(By.id("date")).getAttribute("class");

                System.out.println("classAttribute: " + classAttribute);

               

                Dimension dimension = driver.findElement(By.id("yourname")).getSize();

                System.out.println("with dimension: " + dimension.getWidth());

                System.out.println("height dimension: " + dimension.getHeight());

               

                Point point = driver.findElement(By.id("yourname")).getLocation();

                System.out.println("X location: " + point.getX());

                System.out.println("Y location: " + point.getY());

               

                String pageSource = driver.getPageSource();

                System.out.println("----------------------Page Source---------------------");

                System.out.println(pageSource);

               

                //wait some time before closing

                try {

                        Thread.sleep(7000);

                } catch (InterruptedException ie) {

                }

               

                //close the driver

                driver.quit();

        }

}

 

elements-information-in-webdriver-1
 

All Chapters
Author