How to get text of web element in Selenium

Selenium provides the getText() method to get text a web element. This method can be applied to any visible web element such as buttons, links, checkboxes, or radio buttons except input boxes that has been successfully located using Selenium locators. However, getText() does not work with input elements (for example, text boxes); for those, the getAttribute("value") method should be used instead.

Before fetching text using getText(), ensure that:

  • The element is present in the DOM
  • The element is visible

For getText(), if the element is not present in the DOM, you’ll usually get a NoSuchElementException when locating it.

If the element is present but not visible, getText() will simply return an empty string.

Below is the script for selenium using Java, C# and JavaScript for getting text of web element

getText in Selenium

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class GetTextTest {
WebDriver driver;

@BeforeClass
public void setUp() {
driver = new ChromeDriver();
driver.get("https://example.com");
}

@Test
public void verifyElementText() {
WebElement element = driver.findElement(By.id("exampleElement"));

// Ensure the element is visible
Assert.assertTrue(element.isDisplayed(), "Element is not visible");

// Get the text and assert
String actualText = element.getText();
String expectedText = "Hello World";
Assert.assertEquals(actualText, expectedText, "Text does not match");
}

@AfterClass
public void tearDown() {
driver.quit();
}
}

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace SeleniumTests
{
public class GetTextTest
{
IWebDriver driver;

[SetUp]
public void Setup()
{
driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://example.com");
}

[Test]
public void VerifyElementText()
{
// Locate the element
IWebElement element = driver.FindElement(By.Id("exampleElement"));

// Ensure the element is visible
Assert.IsTrue(element.Displayed, "Element is not visible");

// Get text and assert
string actualText = element.Text;
string expectedText = "Hello World";
Assert.AreEqual(expectedText, actualText, "Text does not match");
}

[TearDown]
public void TearDown()
{
driver.Quit();
}
}
}

const { Builder, By } = require('selenium-webdriver');
const { expect } = require('chai');

describe('Get Text Test', function () {
this.timeout(30000); // optional timeout
let driver;

before(async function () {
driver = await new Builder().forBrowser('chrome').build();
await driver.get('https://example.com');
});

it('should verify element text', async function () {
// Locate the element
let element = await driver.findElement(By.id('exampleElement'));

// Ensure the element is visible
const isVisible = await element.isDisplayed();
expect(isVisible).to.be.true;

// Get text and assert
const actualText = await element.getText();
const expectedText = "Hello World";
expect(actualText).to.equal(expectedText);
});

after(async function () {
await driver.quit();
});
});

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Testingtalkslatest.com - A project by CreativeHub IT Solutions.
Contact Us At: support@testingtalkslatest.com
Our Partner websites - Classified Hub , CodesToolbox , Smart Fitness Guide , CodesToolbox , Testing Forum
Scroll to Top
0
Would love your thoughts, please comment.x
()
x