Hello Folks,
In this post we will learn below topics:
Let’s start:
For example: <a>I Am Inner Text</a>
In above example, texts “I Am Inner Text” between opening and closing tags are called inner text of web element “a”.
We will see an example below:
We will retrieve text “India” from Google’s URL. Inner text “India” is in between div tag. So we need to locate div web element.
Code to retrieve inner text:
WebElement textIndiaWebElement= driver.findElement(By.xpath("//div[@class='logo-subtext']")); String innerText= textIndiaWebElement.getText(); System.out.println("Inner text is :"+innerText);
Output: Inner text is :India
For example: If the web element is like “<p> India </p>”, getText() will return only “India”.
below code will give you same output:
System.out.println(driver.findElement(By.id(“demoLink”)).getText());
System.out.println(driver.findElement(By.id(“demoPara”)).getText());
Output:
This is inner text.
This is inner text.
Example: <someTag></someTag>
Above html tag has no inner text. So, if we call getText() method on tag, it will give you null.`
Interview question:
Print all language names supported by Google:
package MakeSeleniumEasy; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; public class GetTextExample { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","./exefiles/chromedriver.exe"); WebDriver driver= new ChromeDriver(); driver.get("https://www.google.co.in/"); List e=driver.findElements(By.xpath("//div[@id='_eEe']/a")); System.out.println("Languages supported by Google are:"); for(WebElement w:e) System.out.println(w.getText()); driver.quit(); } }
Note: If you run above program directly, You will not get correct fonts. It will print “???”.
You need to do below setting:
Right click on program-> Run configuration-> Common-> Encoding-> Others-> Select UTF-8
Apply and run.
Languages supported by Google are: हिन्दी বাংলা తెలుగు मराठी தமிழ் ગુજરાતી ಕನ್ನಡ മലയാളം ਪੰਜਾਬੀ
I have covered about getAttribute() method in details in this post.
Both methods are used for different purpose.
getAttribute(String attributName) is used to retrieve given attribute value.
getText() is used to retrieve inner text on web element.
Consider below html code:
In above code, input tag has three attributes named “attr1”, “attr2” and “attr3” and also has innerText “foo”.
Consider below codes:
getAttribute(attr1)) will give you "Value1". getAttribute(attr2)) will give you "Value2". getAttribute(attr3)) will give you "Value3". getText()) will give you "foo".
Only scenario when getAttribute() and getText() will return you same value in below html:
For above html code:
getAttribute(“value”) will give you “foo”.
getText() will also give you “foo”.
I hope you must have learnt new things through this post. If you like my posts, please like, comment, share ans subscribe.
#HappyLearning
My name is Amod Mahajan and I am an IT employee with 4+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went throw so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning
Hello Folks, Previously, I had published on Handling StaleElementReferenceException using PageFactory. But many people asked how to handle it if they…
Hello Guys, As I say always, your automation script is ineffective if you do not include logic to validate to…
"How much Java I need to learn for selenium with Java binding?" is mostly asked question by a Professional who…
Hello Guys, You should not be able to type alphabets or special characters in a field which supposed to accept…
Hello Folks, Recently a guy asked me this question which he was asked in an interview in IBM. What the…
Hello Folks, As part of our API Testing series, we will see “Sending GET request with params in Postman”. In last…