1

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
1

3 Answers 3

6

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#.

0
0

Try this it will work

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

I'm using return arguments[0].innerHTML

0

I recommand to use:

JavascriptExecutor js = (JavascriptExecutor)element;
String content=(String) js.executeScript("return document.getElementsByClassName('hide').value;");
System.out.println(content);

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.