Selenium (java), Ignore ‘Undefined’ JavaScript error

I am trying to write tests for my online shopping page. I have a JS command that tests discounts that I would like to execute using Selenium. When I run the Java script code in a Chrome browser console, it works flawlessly, however when I try to execute it using Selenium I get an error because the JS method is undefined (Which it is, I am getting a message about that in the browser console as well, however the browser ignores it and continues the execution nevertheless). When Selenium finds the undefined error, it quits the application without executing the JS command.

Is there a way to make Selenium ignore the javascript errors and continue executing it (just like the chrome console does)?

I am using Selenium-java-3.0.0 with the latest Chrome driver. My Java code is executed using the latest Eclipse Neon which is in turn run on a Windows 8.1 machine.

The relevant code that I am trying to run is:
public WebDriver driver = new ChromeDriver();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(“addFakeRule(\”74910\”, {isNew: true});”);

The Error I am getting is:
org.openqa.selenium.WebDriverException: unknown error: addFakeRule is not defined
(Session info: chrome=54.0.2840.71)
(Driver info: chromedriver=2.24.417431 (9aea000394714d2fbb20850021f6204f2256b9cf),platform=Windows NT 6.3.9600 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 40 milliseconds
Build info: version: ‘unknown’, revision: ‘350cf60’, time: ‘2016-10-13 10:43:56 -0700’
System info: host: ‘MMA-CZC3094Z8B’, ip: ‘’, ‘Windows 8.1’, os.arch: ‘amd64’, os.version: ‘6.3’, java.version: ‘1.8.0_111’
Driver info:
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.24.417431 (9aea000394714d2fbb20850021f6204f2256b9cf), userDataDir=c:\temp\scoped_dir3536_18951}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=54.0.2840.71, platform=WIN8_1, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 6ba9f4715531bdd6ae439ac07665e101
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(
at org.openqa.selenium.remote.RemoteWebDriver.execute(
at org.openqa.selenium.remote.RemoteWebDriver.executeScript(
at IKEA_Test.TestCases.testDiscounts(
at IKEA_Test.InitiateTest.testDiscounts(
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(
at org.testng.internal.Invoker.invokeMethod(
at org.testng.internal.Invoker.invokeTestMethod(
at org.testng.internal.Invoker.invokeTestMethods(
at org.testng.internal.TestMethodWorker.invokeTestMethods(
at org.testng.TestRunner.privateRun(
at org.testng.SuiteRunner.runTest(
at org.testng.SuiteRunner.runSequentially(
at org.testng.SuiteRunner.privateRun(
at org.testng.SuiteRunnerWorker.runSuite(
at org.testng.TestNG.runSuitesSequentially(
at org.testng.TestNG.runSuitesLocally(
at org.testng.TestNG.runSuites(
at org.testng.remote.RemoteTestNG.initAndRun(
at org.testng.remote.RemoteTestNG.main(

Default Asked on October 24, 2016 in Selenium WebDriver.
Add Comment
  • 0 Answer(s)
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.