nautical nautical - 3 months ago 18
Javascript Question

Selenium IDE fails getElementById()

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?

Answer

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";