Is there any way to get the text of a web element using Selenium Webdriver?

0 votes
May 29, 2019 in Selenium by Komal
4,710 views

2 answers to this question.

0 votes

Hey Komal, to get the text of a web element in Selenium Webdriver you can use getText() method:

package getTextPackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class getTextTest {

 @Test
 public void testmethod(){
  System.setProperty("webdriver.chrome.driver", "C:\\Downloads\\Drivers\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("https://www.google.com");
    String availableText = driver.findElement(By.xpath("//*[@id='gbw']/div/div/div[1]/div[1]/a")).getText();
    System.out.println("Text Available is :"+availableText);
 }
}
answered May 29, 2019 by Anvi
• 14,110 points
0 votes

use gettext()

in java : string lableText = driver.findElement(By.xpath("//*[@id='lable1")).getText();


WebDriver driver = new FirefoxDriver();
WebElement theSpan = driver.findElement(By.id("spannyspan"));
String title = theSpan.getAttribute("title");
String label = theSpan.getText();
answered Sep 3 by Sri
• 2,970 points

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer
0 votes
2 answers

Is it possible to manually set the attribute value of a Web Element using Selenium?

WebDriver driver; JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("document.getElementById('id123').setAttribute('attr', ...READ MORE

answered Sep 3 in Selenium by Sri
• 2,970 points
16,373 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,520 points
4,027 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31 in Selenium by Sri
• 2,970 points
4,976 views
0 votes
2 answers

What is the role of TestNG & JUnit frameworks in Selenium?

TestNG and JUnit are test frameworks . it ...READ MORE

answered Sep 4 in Selenium by Sri
• 2,970 points
931 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,890 points
2,917 views
0 votes
1 answer
0 votes
1 answer