asked 2 years ago
To get the visible text from a label, paragraph, or any other element in Selenium Java, use the getText() method. This method retrieves the inner text of the element, which can then be used for validation or printing.
Navigate to https://www.qafeast.com/demo,click the Label menu.
Press F12 on the keyboard and inspect the Label.
Syntax:
Example:
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;
public class TextValidationExample {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("https://www.qafeast.com/demo"); // Example URL
// Locate the label or paragraph element
WebElement label = driver.findElement(By.xpath("//h3[text()='Test automation']"));
// Get the text from the element
String actualText = label.getText();
// Validate the text
String expectedText = "Test automation";
Assert.assertEquals(actualText, expectedText, "Text does not match!");
driver.quit();
}
}