How to extract the text of a webelement in Selenium?

Software TestingAutomation TestingSelenium Web Driver

Selenium extracts the text of a webelement with the help of getText() method.

Code Implementation with getText().

Example

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class ExtractText {
   public static void main(String[] args) {
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      String url = "https://www.tutorialspoint.com/index.htm";
      driver.get(url);
      driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
      //Using id with # for css expression
      driver.findElement(By.cssSelector("#gsc-i-id1")).sendKeys("Selenium");
      // extracting the text entered in console with getText()
      System.out.println(“The entered text is:” driver.findElement(By.cssSelector("#gsc-i-         id1")).getText());
      driver.close();
   }
}
raja
Published on 10-Jun-2020 16:05:36
Advertisements