Reputation: 35
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
Reputation: 26
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
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
Reputation: 35
(String) ((JavascriptExecutor) this)
.executeScript("return window.document.getElementById('tempid').innerHTML");
Upvotes: -1