nautical nautical - 1 year ago 170
Javascript Question

Selenium IDE fails getElementById()

I have the following statement in my

Selenium IDE


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>

function myFunction() {
document.getElementById("demo").innerHTML = "Hello World";

There is no problem when I execute just


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

Answer Source

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";
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download