4

For some reason, I cannot execute any javascript using "executescript" when I run my functional tests through IE. It works with Firefox. I have searched several hours on google with no luck.

I am simply calling

browser.driver.executeScript("console.log('test');")

or

JavascriptExecutor js = (JavascriptExecutor) driver
driver.executeScript("console.log('test');")

or whatever variation you please to call the executeScript method.

The stacktrace I am getting is:

org.openqa.selenium.WebDriverException: JavaScript error (WARNING:
The server did not provide any stacktrace information)
Command duration or timeout: 164 milliseconds
Build info: version: '2.37.1', revision: 'a7c61cbd68657e133ae96672cf995890bad2ee42',           
time: '2013-10-21 09:08:07'
System info: host: 'functionalTests', ip: '10.22.6.112', os.name: 'Windows 8', os.arch:    
'x86', os.version: '6.2', java.version: '1.6.0_45'
Session ID: 8b04c740-07a0-4678-a1b6-aacd56268625
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{platform=WINDOWS, javascriptEnabled=true, elementScrollBehavior=0,     
enablePersistentHover=true, ignoreZoomSetting=false, ie.ensureCleanSession=false,   
browserName=internet explorer, enableElementCacheCleanup=true,   
unexpectedAlertBehaviour=dismiss, version=11, ie.usePerProcessProxy=false, 
cssSelectorsEnabled=true, ignoreProtectedModeSettings=false, requireWindowFocus=false,  
initialBrowserUrl=http://localhost:17553/, handlesAlerts=true,  
ie.forceCreateProcessApi=false, nativeEvents=true, browserAttachTimeout=0, 
ie.browserCommandLineSwitches=, takesScreenshot=true}]
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
at  
org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
at 
org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:463)
at functional.utility.JQInstaller.installJQ(JQInstaller.groovy:16)
at functional.spec.NavigateAccountSettingsSpec.Navigate to the Account 
Settings(NavigateAccountSettingsSpec.groovy:39)

It is not a problem with my "JQInstaller" class, because I took that out and just tried console.log functions. It is a problem with this "executeScript" method. I am using Selenium 2.37.1.

jcd
  • 221
  • 4
  • 15
  • 2
    What version of IE are you using? IE8 and below do not have console.log – Paul Harris Dec 31 '13 at 19:43
  • I am using 9,10, and 11 – jcd Jan 02 '14 at 16:30
  • It's problem only with IE? Test pass if you run Firefox browser? – plsgogame Jan 04 '14 at 11:30
  • Yes, that's correct. I cant run any semblance of "executeScript" in IE. – jcd Jan 04 '14 at 16:00
  • @jcd, did you solve this? i'm troubleshooting the same, now on 2.39.0 – Leo Gallucci Jan 23 '14 at 04:19
  • @elgalu, No, for now we have just been sticking to firefox. – jcd Jan 23 '14 at 17:38
  • This is weird, but dev guys uploaded some missing png files and after that the eval is working fine and all tests green on IE10. Check if your page is loading unexisting resources and let know, if you find time. Cheers – Leo Gallucci Jan 23 '14 at 19:09
  • 1
    @elgalu, We have no missing resources on that page. Bummer, I was hoping that would be it. – jcd Jan 25 '14 at 00:50
  • 1
    I have the same issue, using Selenium 2.40.0, I reported it: http://code.google.com/p/selenium/issues/detail?id=7135&thanks=7135&ts=1395925142 – Fried Hoeben Mar 27 '14 at 13:26
  • 1
    You say "nothing works," but your example will error if the F12 Developer Tools window is not open (accessing "console." if the F12 tools aren't open throws an error in at least IE < 10). What happens if you do something like `js.executeScript("return 1+1;");`? – JimEvans Mar 27 '14 at 18:27
  • If you are accessing a page that is local to your computer, i.e. file path is "C:\", Internet Explorer might be preventing the execution of javascript. See: http://stackoverflow.com/questions/7038724/how-to-automaticaly-allow-blocked-content-in-ie – el_stack Jul 23 '14 at 15:35

2 Answers2

1

Have you tried the Enumerable version?

IJavaScriptExecutor js = WebDriver.driver as IJavaScriptExecutor;

if (js != null) {
     value = (string)js.ExecuteScript(javascriptArgumentAsString, element);
}
Asyranok
  • 950
  • 1
  • 7
  • 17
  • Sorry that was a get, here is the set; – Asyranok May 23 '14 at 22:50
  • IJavaScriptExecutor js = WebDriver.driver as IJavaScriptExecutor; if (js != null) { js.ExecuteScript(javascriptArgumentStringSet(attribute, value), element); } – Asyranok May 23 '14 at 22:50
  • Actually, I can see the issue, I think. You are doing driver.ExecuteScript when it should be js.ExecuteScript. – Asyranok May 23 '14 at 22:51
1

IE 7 and below do not have console.log.

IE 8, 9 and 10 have it but dev tools must be open. Open these with F12.

IE 11 works fine.

Each IE browser has a script debug window which can be activated in IE tools menu. This would have caused an alert to show you the reason for failure in older IE browsers.

If you want extra help understanding this look here: Specifically at Walter's answer for an excellent solution

Community
  • 1
  • 1
twinj
  • 2,009
  • 18
  • 10