Harish Naidu
Harish Naidu

Reputation: 35

Not able to get visible text from some paragraph by using javascript executor and web driver

In a site I have:

<p id="tempid" value="Manual Effect">testing the test</p>
String value = (String)((JavascriptExecutor) this).executeScript("return window.document.getElementById('tempid').value");
System.out.println("value is : " + value);

I am getting null value or nothing.
I want to get the test "testing the test" as output.

Upvotes: -3

Views: 570

Answers (3)

Vaibhav Deshmukh
Vaibhav Deshmukh

Reputation: 26

testing the test

Sir, You try this solution...

String script = "return document.getElementByTagName('p').tempid.getInnerHtml()";

String script1 = "return document.getElementById('tempid').innerHTML;

JavaScriptExecutor js = (JavaScriptExecutor)driver;

String value = (String)js.executeScript(script);

System.out.println(value);

JavascriptExecutor is more better than webdriver ant many assepect. WebDriver are work on the basis of webElements, some time if webElement is not accessible. It Show staleElementException, ElementNotFoundException..., Its some time headache.

But JavaScript Executor work directly On HTML. So that why its better...

Upvotes: 0

nilesh
nilesh

Reputation: 14287

Like everyone already mentioned. Don't use Javascript. If you do, you don't need WebDriver.

WebElement para = driver.findElement(By.id("tempid"));
String text = para.getText(); //this will return "testing the test"
String value = para.getAttribute("value"); //this will return "Manual Effect"

Upvotes: 0

Harish Naidu
Harish Naidu

Reputation: 35

(String) ((JavascriptExecutor) this)
                    .executeScript("return window.document.getElementById('tempid').innerHTML");

Upvotes: -1

Related Questions