-3

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.

Cerbrus
  • 70,800
  • 18
  • 132
  • 147
Harish Naidu
  • 35
  • 1
  • 12

3 Answers3

0

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"
nilesh
  • 14,131
  • 7
  • 65
  • 79
0

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

-1
(String) ((JavascriptExecutor) this)
                    .executeScript("return window.document.getElementById('tempid').innerHTML");
Harish Naidu
  • 35
  • 1
  • 12
  • 1
    Simple use webdriver.findElement(By.Id('tempid')) . Why are you using Javascript? It is also visible. Its not a good practice. – A Paul Feb 03 '14 at 12:59
  • Actually i tried with that also but i am not creating an object just inheriting the firefox driver so i used super word but it throwing error super is a reserved identifier – Harish Naidu Feb 03 '14 at 13:10
  • Show us the code what you are doing and the stack trace. – A Paul Feb 04 '14 at 05:05