0

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

3
  • Note that by calling .click() you are no longer simulating a user click and no longer writing an end to end test. The click from Javascript simply executes the underlying listener or attached redirection which doesn't validate that a real user will be able to click the targeted element. Regarding you error, let is not supported by IE11 and should be replaced by var. Commented May 4, 2018 at 11:37
  • Hello and thanks for the response. I know calling the .click() is not ideal but it's the only solution I've got. As for what I'm clicking I make sure it's a clickable element. I will try to replace let by var asap! Commented May 4, 2018 at 12:02
  • No, even replacing let by var doesn't change anything Commented May 4, 2018 at 12:13

1 Answer 1

1

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') {
}
Sign up to request clarification or add additional context in comments.

2 Comments

Let me know if this helped
Hello and thanks. Unfortunately, having the window fully loaded and putting the script in a function didn't solve it :/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.