2

I have the following statement in my Selenium IDE script: I store javascript{window.document.getElementById('demo').innerHTML} text

But this always fails with the error

[info] Executing: |store | javascript{window.document.getElementById('demo').innerHTML} | text | [error] Unexpected Exception: TypeError: window.document.getElementById(...) is null.

The HTML source code I am using is from w3schools. The corresponding HTML source is

<p id="demo">Click the button to change the text in this paragraph.</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
    document.getElementById("demo").innerHTML = "Hello World";
}
</script>

There is no problem when I execute just

<tr>
    <td>store</td>
    <td>javascript{window.document.innerHTML}</td>
    <td>text</td>
</tr>

but trying to retrieve an element by its id is not working. What am I missing?

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • Actually you are using main document while desire I'd element is inside an iframe you need to call `getElementById` on iframe document...as `window.frame["demo_frame"].document.getElementById("demo").innerHTML = "Hello World";`...:) – Saurabh Gaur Aug 28 '16 at 15:08
  • @SaurabhGaur This just returns `[error] Threw an exception: window.frame is undefined`. –  Aug 28 '16 at 15:49
  • Sorry it `window.frames`...just typo mistake.. – Saurabh Gaur Aug 28 '16 at 16:16
  • @SaurabhGaur It is still failing: `[error] Threw an exception: window.frames.iframeResult.document is undefined`. I also tried with `frames[0]` and `frames[1]`. It then throws the `null` exception again. –  Aug 28 '16 at 16:24
  • Try once as `document.getElementById("iframe Id").contentDocument.getElementById("demo").i‌​nnerHTML = "Hello World";` and let me know...:) – Saurabh Gaur Aug 28 '16 at 16:28
  • 1
    @SaurabhGaur Your last suggestion worked. If you want to make your last comment an answer then I will accept it. Thanks. –  Aug 28 '16 at 17:12
  • Ya sure, I have provided it as an answer, you can accept it. Thanks..:) – Saurabh Gaur Aug 28 '16 at 17:17

1 Answers1

0

Actually you are using main document while desire I'd element is inside an iframe you need to call getElementById on iframe document as below :-

document.getElementById("iframe Id").contentDocument.getElementById("demo").i‌​nnerHTML = "Hello World";
Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • please could u help me here: https://stackoverflow.com/questions/47944349/selenium-cannot-click-on-an-element-inside-a-modal I am struggling since 2 days – eeadev Dec 29 '17 at 10:07