0

Here I want to get extract the value from the tag. But through the selenium, I am not able to find script tag and fetch data for the same.

But through Javascript, I am able to access that script tag and fetch the value. The script is like below,

<script class="payload-script"> Text </script>

$(".payload-script").text();

but now I want to run this script through JavascriptExecutor and get value to java variable so I can do further processing.

1
  • What's the code you have tried and what was the output of that ? Commented Jul 24, 2021 at 9:12

1 Answer 1

1

In selenium - JavaScriptExecutor is an interface that provides a mechanism to execute Javascript through selenium driver.

Please use the below code:

JavascriptExecutor jse = (JavascriptExecutor) driver;
String textValue = (String) jse.executeScript("return document.getElementsByClassName('payload-script')[0].text");
System.out.println(textValue);

Another way:

WebElement el = driver.findElement(By.className("payload-script"));
String textValue2 = (String) jse.executeScript("return arguments[0].text", el);
System.out.println(textValue2);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.