Lucky-Luke87
Lucky-Luke87

Reputation: 19

Selenium C# IJavaScriptExecutor throws error but JS is fine

I'm using Selenium C# to test a pretty complex web UI in Internet Explorer 11. As you might know, Selenium's Click() tends to not work in which case inserting a JS click method is necessary. I'm running the dynamically generated script below using

(IJavaScriptExecutor) driver).ExecuteScript(script). Here is the script :

let iFrame = document.getElementById("dkwframe").contentWindow.document;
let element = iFrame.querySelector("[id*='_ImgLnkNewPage_LinkButtonControl']");
element.click();

The script works fine when I execute it directly in the IE console, but when executing with it Selenium I get this :

System.InvalidOperationException : Error executing JavaScript (UnexpectedJavaScriptError)

The IE console is empty so I don't think it's even trying. Also, switching browser isn't an option.

Thanks for the help

Upvotes: 0

Views: 636

Answers (1)

JacopoDT
JacopoDT

Reputation: 365

Maybe the script is being executed before the page is fully loaded ,try to put it in a page ready event ha dler like that

window.onload = function() {
    et iFrame = document.getElementById("dkwframe").contentWindow.document;
    let element = iFrame.querySelector("[id*='_ImgLnkNewPage_LinkButtonControl']");
    element.click();
}

Or you can check if the fully loaded with :

if (document.readyState === 'complete') {
}

Upvotes: 1

Related Questions