'Selenium Webdriver - how to get the value in span tag

<span id="email-display">myemailid</span>

How could i get the value between the span tag in selenium webdriver? I tired using this code:

System.out.println(driver.findElement(By.xpath(".//*[@id='email-display']")).getText());

But i am not able to get the value. What has to be done? Thanks in advance.



Solution 1:[1]

Try this:

System.out.println(driver.findElement(By.xpath(".//*[@id='email-display']")).getAttribute("textContent")

Solution 2:[2]

use this:

System.out.println(driver.findElement(By.cssSelector("span#email-display")).getText());

if this can not get text, please paste html with parent root and let me know.

Solution 3:[3]

You can use

 driver.findElement(By.id("email-display")).getText();
 driver.findElement(By.xpath("//span[@id='email-display']")).getText();
 driver.findElement(By.cssSelector("span#email-display")).getText();

If that element is in new window or in frame, then switch to that window / frame, then perform the operations.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 levis84
Solution 2 noor
Solution 3 Rohit Ware