Search code examples
htmlseleniumwebdriverhidden-field

Getting a hidden text from a HTML in JAVA using Selenium and WebDriver


I'm trying to get a text from a html page which is into a hidden span.

<span class="hide"> Nike </span>

I've tried something like this:

WebElement element = driver.findElement(By.cssSelector(".product-page.clearfix > .hide > span"));
String content = (String)((JavascriptExecutor)driver).executeScript("arguments[0].innerHTML;", element);
product.setBrand(content)

I've tried also with "return arguments[0].innerHTML;" and with "element.getText()"

I understood that I can use pure javascript, but this is a simple example, I need to use WebDriver and Java code for more complicated pages.

java.lang.ClassCastException: package.driver.DhlWebDriver cannot be cast to org.openqa.selenium.JavascriptExecutor

Solution

  • You just need to get a proper attribute like following:
    element.GetAttribute("textContent");
    Should work well.

    I guess getAttribute for JAVA - I work in C#.