Get Text of an Element in Selenium webdriver

getText() method fetches text present in the html including sub-elements text , getText() returns value as string


										import org.openqa.selenium.By;
										import org.openqa.selenium.WebDriver;
										import org.openqa.selenium.chrome.ChromeDriver;

										public class GetText {
											public static void main(String[] args) {
												// set chrome driver exe path
												System.setProperty("webdriver.chrome.driver", "C:/path/chromedriver.exe");
												WebDriver driver = new ChromeDriver();
												driver.get("https://chercher.tech/selenium-webdriver-sample");
												// fetches text from the element and stores it in text
												String text = driver.findElement(By.tagName("div")).getText();
												System.out.println("Text present in span : "+ text);
											}
										}

									


										output: Text present in span : selenium webdriver
										

We cannot extract text from the textbar or any editable item using getText(), to get text from the editable we have to use getAttribute("value")


To get text value from the textbars we need to use getAttribute("value")


										import org.openqa.selenium.By;
										import org.openqa.selenium.WebDriver;
										import org.openqa.selenium.chrome.ChromeDriver;

										public class GetAttributeNoValue {
											public static void main(String[] args) {
												// set chrome driver exe path
												System.setProperty("webdriver.chrome.driver", "C:/path/chromedriver.exe");
												WebDriver driver = new ChromeDriver();
												driver.get("https://chercher.tech/selenium-webdriver-sample");
												// clicks the button which has id = 'button'
												String valueAttribute = driver.findElement(By.id("getA")).getAttribute("value");
												System.out.println("Attribute of value is : "+ valueAttribute);
											}
										}
									

getAttribute method in selenium webdriver

getAttibute() method fetches the value of an attribute, in html code whatever is present in left side of '=' is attribute, value on the right side is Attribute value


						import org.openqa.selenium.By;
						import org.openqa.selenium.WebDriver;
						import org.openqa.selenium.chrome.ChromeDriver;

						public class GetAttribute {
							public static void main(String[] args) {
								// set chrome driver exe path
								System.setProperty("webdriver.chrome.driver", "C:/path/chromedriver.exe");
								WebDriver driver = new ChromeDriver();
								driver.get("https://chercher.tech/selenium-webdriver-sample");
								// clicks the button which has id = 'button'
								String idAttribute = driver.findElement(By.tagName("button")).getAttribute("id");
								System.out.println("Attribute of Id is : "+ idAttribute);
							} 
						}
					



Insights


1. getAttibute() returns the value at the time of query, selenium will not return new value if the Attribute value changes after page load


										import org.openqa.selenium.By;
										import org.openqa.selenium.WebDriver;
										import org.openqa.selenium.chrome.ChromeDriver;

										public class GetAttributeNoValue {
											public static void main(String[] args) {
												// set chrome driver exe path
												System.setProperty("webdriver.chrome.driver", "C:/path/chromedriver.exe");
												WebDriver driver = new ChromeDriver();
												driver.get("https://chercher.tech/selenium-webdriver-sample");
												// clicks the button which has id = 'button'
												String idAttribute = driver.findElement(By.tagName("button")).getAttribute("id");
												System.out.println("Attribute of Id is : "+ idAttribute);
											}
										}
									


2. getAttibute() returns blank as value if attribute is not set to any value


										import org.openqa.selenium.By;
										import org.openqa.selenium.WebDriver;
										import org.openqa.selenium.chrome.ChromeDriver;

										public class GetAttributeNoValue {
											public static void main(String[] args) {
												// set chrome driver exe path
												System.setProperty("webdriver.chrome.driver", "C:/path/chromedriver.exe");
												WebDriver driver = new ChromeDriver();
												driver.get("https://chercher.tech/selenium-webdriver-sample");
												// return blank as there is no such value
												String nameAttribute = driver.findElement(By.tagName("button")).getAttribute("name");
												System.out.println("Attribute of name is : "+ nameAttribute);
											}
										}
									


3. getAttibute() returns 'true' incase if you are searching for boolean value

These are few boolean attributes :     async, autofocus, autoplay, checked, compact, complete, controls, declare, defaultchecked, defaultselected, defer, disabled, draggable, ended, formnovalidate, hidden, indeterminate, iscontenteditable, ismap, itemscope, loop, multiple, muted, nohref, noresize, noshade, novalidate, nowrap, open, paused, pubdate, readonly, required, reversed, scoped, seamless, seeking, selected, spellcheck, truespeed, willvalidate Finally


										import org.openqa.selenium.By;
										import org.openqa.selenium.WebDriver;
										import org.openqa.selenium.chrome.ChromeDriver;

										public class GetAttributeNoValue {
											public static void main(String[] args) {
												// set chrome driver exe path
												System.setProperty("webdriver.chrome.driver", "C:/path/chromedriver.exe");
												WebDriver driver = new ChromeDriver();
												driver.get("https://chercher.tech/selenium-webdriver-sample");
												// clicks the button which has id = 'button'
												String checkedAttribute = driver.findElement(By.tagName("button")).getAttribute("checked");
												System.out.println("Attribute of checked is : "+ checkedAttribute);
											}
										}
									


4. getAttibute() returns 'null' if there no such attribute


										import org.openqa.selenium.By;
										import org.openqa.selenium.WebDriver;
										import org.openqa.selenium.chrome.ChromeDriver;

										public class GetAttributeNoValue {
											public static void main(String[] args) {
												// set chrome driver exe path
												System.setProperty("webdriver.chrome.driver", "C:/path/chromedriver.exe");
												WebDriver driver = new ChromeDriver();
												driver.get("https://chercher.tech/selenium-webdriver-sample");
												// clicks the button which has id = 'button'
												String bankerAttribute = driver.findElement(By.tagName("button")).getAttribute("banker");
												System.out.println("Attribute of banker is : "+ bankerAttribute);
											}
										}
									

Get CSS Value

getCssValue method in selenium webdriver fetches the value of an css property of an webelement, we have to pass what property selenium wedriver have to fetch.

@param propertyName : the css property name of the element
@return The current computed value of the property.

Refer below image for css properties
css values in selenium webdriver


										import org.openqa.selenium.By;
										import org.openqa.selenium.Dimension;
										import org.openqa.selenium.WebDriver;
										import org.openqa.selenium.chrome.ChromeDriver;

										public class GetElementSize {
											public static void main(String[] args) {
												// set chrome driver exe path
												System.setProperty("webdriver.chrome.driver", "C:/path/chromedriver.exe");
												WebDriver driver = new ChromeDriver();
												driver.get("https://chercher.tech/selenium-webdriver-sample");
												// fetches text from the element and stores it in text
												Dimension size = driver.findElement(By.xpath("//button[contains(@class,'btn-primary')]")).getSize();
												System.out.println("Size of the button : "+ size);
											}
										}
									


										output: Size of the button : (76, 38)
									


Whatever values present in images is not matching with output of the program, this happens because we have not passed correct value or may be because our browser gave wrong information about the values.

If we use shorthand properties like margin selenium webdriver may not return accurate value, so whenever we want to check css values we have give long hand properties like margin-top, margin-left, padding-top.

Solution : Whenever you verify any css property you should check the values in Computed tab
computed css values in selenium webdriver


										import java.util.concurrent.TimeUnit;
										import org.openqa.selenium.By;
										import org.openqa.selenium.WebDriver;
										import org.openqa.selenium.chrome.ChromeDriver;

										public class GetCSSValues {
											public static void main(String[] args) {
												// set chrome driver exe path
												System.setProperty("webdriver.chrome.driver", "C:/path/chromedriver.exe");
												WebDriver driver = new ChromeDriver();
												driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
												driver.get("https://chercher.tech/selenium-webdriver-sample");
												String bgColor = driver.findElement(By.xpath("//button[contains(@class,'btn-primary')]")).getCssValue("background-color");
												String color = driver.findElement(By.xpath("//button[contains(@class,'btn-primary')]")).getCssValue("border-bottom-color");
												String borderBottomWidth = driver.findElement(By.xpath("//button[contains(@class,'btn-primary')]")).getCssValue("border-bottom-width");
												System.out.println("Css Value for background color is : "+ bgColor);
												System.out.println("Css Value for color is : "+ color);
												System.out.println("Css Value for border bottom color is : "+ borderBottomWidth);
											}
										}
									


										Css Value for background color is : rgba(0, 123, 255, 1)
										Css Value for color is : rgba(0, 123, 255, 1)
										Css Value for border bottom color is : 1px
									

Css values are depends on the browser, you may not get same values for all the browser,below are few such examples

Few browsers may show 1px and other may show 1 px [ there is space in difference ]
Few browser may show rgba(0, 123, 255, 1) and other may show rgb(0, 123, 255) [ rgb and rgba difference]

Please find the example for such different values in different browsers. Please read all tutorials in sequence for better understanding

Get Size of Element in Selenium Webdriver

getSize() method in selenium webdriver determines the size of an element, size consists of two values width and height which are sum of respective attributes

@parameters : accepts nothing
@return : Dimension class

Width = margin-left + margin-right + padding-left + padding-right + actual width; width get size in selenium webdriver

Height = margin-top + margin-bottom + padding-top + padding-bottom + actual height; height get size in selenium webdriver


										import org.openqa.selenium.By;
										import org.openqa.selenium.Dimension;
										import org.openqa.selenium.WebDriver;
										import org.openqa.selenium.chrome.ChromeDriver;

										public class GetElementSize {
											public static void main(String[] args) {
												// set chrome driver exe path
												System.setProperty("webdriver.chrome.driver", "C:/path/chromedriver.exe");
												WebDriver driver = new ChromeDriver();
												driver.get("https://chercher.tech/selenium-webdriver-sample");
												// fetches text from the element and stores it in text
												Dimension size = driver.findElement(By.xpath("//button[contains(@class,'btn-primary')]")).getSize();
												System.out.println("Size of the button : "+ size);
											}
										}
							


										output: Css Value for background color is is : rgba(0, 123, 255, 1)
									


getSize() method returns Dimension class object which have constants like height and width, and methods like getHeight(),getWidth(), using those methods and constants we can get the required property either height or width.


										import org.openqa.selenium.By;
										import org.openqa.selenium.Dimension;
										import org.openqa.selenium.WebDriver;
										import org.openqa.selenium.chrome.ChromeDriver;

										public class GetHeightWidth {
											public static void main(String[] args) {
												// set chrome driver exe path
												System.setProperty("webdriver.chrome.driver", "C:/path/chromedriver.exe");
												WebDriver driver = new ChromeDriver();
												driver.get("https://chercher.tech/selenium-webdriver-sample");
												// fetches text from the element and stores it in text
												Dimension size = driver.findElement(By.xpath("//button[contains(@class,'btn-primary')]")).getSize();
												
												System.out.println("Size of the button : "+ size);
												
												System.out.println("Width of the button with constant : "+ size.width);
												System.out.println("Height of the button with constant : "+ size.height);
												System.out.println("Width of the button with method: "+ size.getWidth());
												System.out.println("Height of the button with method: "+ size.getHeight());
											}
										}
									


										Size of the button : (76, 38)
										Width of the button with constant : 76
										Height of the button with constant : 38
										Width of the button with method: 76
										Height of the button with method: 38
									

Is Enabled

isEnabled() method in selenium webdriver verifies and returns a boolean value based on the state of the element whether it is enabled or not.

If there is no such attribute as enabled, selenium webdriver considers that the element is enabled If there is disabled attribute and it set to true then selenium webdriver return false


										import org.openqa.selenium.By;
										import org.openqa.selenium.WebDriver;
										import org.openqa.selenium.chrome.ChromeDriver;

										public class IsEnabled {
											public static void main(String[] args) {
												// set chrome driver exe path
												System.setProperty("webdriver.chrome.driver", "C:/path/chromedriver.exe");
												WebDriver driver = new ChromeDriver();
												driver.get("https://chercher.tech/selenium-webdriver-sample");
												// verify whether button is enabled or not ?
												boolean enabled = driver.findElement(By.xpath("//button[@disabled]")).isEnabled();
												System.out.println("Element State is : "+ enabled);
											}
										}
									


										output: Element State is : false
										


Sometime in few applications you may not even get the value of an element whether it is enabled or disabled, those time you need to inspect the element and find the attribute which makes element disabled.

Once we know the attribute which makes the element disable or enable try to get that attribute value and use it in condition block to fail or pass the test, in this case return type is String rather than boolean


										import org.openqa.selenium.By;
										import org.openqa.selenium.WebDriver;
										import org.openqa.selenium.chrome.ChromeDriver;

										public class IsEnabledWithoutAttribute {
											public static void main(String[] args) {
												// set chrome driver exe path
												System.setProperty("webdriver.chrome.driver", "C:/path/chromedriver.exe");
												WebDriver driver = new ChromeDriver();
												driver.get("https://chercher.tech/selenium-webdriver-sample");
												// verify whether button is enabled or not ?
												String enabled = driver.findElement(By.xpath("//button[@disabled]")).getAttribute("disabled");
												System.out.println("Element State without actual attribute: "+ enabled);
											}
										}
									


										output: Element State without actual attribute: true
										

Is Displayed

isDisplayed method in selenium webdriver verifies and returns a boolean value based on the state of the element whether it is displayed or not.

If there is no such attribute as hidden, selenium webdriver considers that the element is displayed (visit Boolean Attribute for details) and returns true.

If there is hidden attribute and it set to true then selenium webdriver return false as it is hidden


										import org.openqa.selenium.By;
										import org.openqa.selenium.WebDriver;
										import org.openqa.selenium.chrome.ChromeDriver;

										public class IsDisplayed {
											public static void main(String[] args) {
												// set chrome driver exe path
												System.setProperty("webdriver.chrome.driver", "C:/path/chromedriver.exe");
												WebDriver driver = new ChromeDriver();
												driver.get("https://chercher.tech/selenium-webdriver-sample");
												// verify whether button is displayed or not ?
												boolean displayed = driver.findElement(By.xpath("//button[@id='displayed']")).isDisplayed();
												System.out.println("Element Displayed is : "+ displayed);
											}
										}
									


										output: Element Displayed is : false
										

Is Selected

isSelected() verifies if an element is selected or not. isSelected() returns boolean value, true if the element is selected and false if it is not.

It is widely used on check boxes, radio buttons and dropdowns in selenium webdriver.


										import org.openqa.selenium.By;
										import org.openqa.selenium.WebDriver;
										import org.openqa.selenium.chrome.ChromeDriver;

										public class IsSelected {
											public static void main(String[] args) {
												// set chrome driver exe path
												System.setProperty("webdriver.chrome.driver", "C:/path/chromedriver.exe");
												WebDriver driver = new ChromeDriver();
												driver.get("https://chercher.tech/selenium-webdriver-sample");
												// verify whether checkbox is selected or not ?
												boolean selected = driver.findElement(By.xpath("//input[@id='selected']")).isSelected();
												System.out.println("Is checkbox selected ? : "+ selected);
											}
										}
									


Radio button example


										import org.openqa.selenium.By;
										import org.openqa.selenium.WebDriver;
										import org.openqa.selenium.chrome.ChromeDriver;

										public class IsSelectedRadio {
											public static void main(String[] args) {
												// set chrome driver exe path
												System.setProperty("webdriver.chrome.driver", "C:/path/chromedriver.exe");
												WebDriver driver = new ChromeDriver();
												driver.get("https://chercher.tech/selenium-webdriver-sample");
												// verify whether checkbox is selected or not ?
												boolean selected = driver.findElement(By.xpath("//input[@id='radio']")).isSelected();
												System.out.println("Is Radio button selected ? : "+ selected);
											}
										}
									


										output: Element State without actual attribute: true
										

 
Join My Facebook Group
Join Group
 

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
 
Selenium-Webdriver.Com [Group]
Facebook Group · 801 members
Join Group
Learn Selenium Webdriver Concepts and Interview Questions Learn and code
 
Copyright © CherCher Tech