0

I am working on selenium automation using java. Selenium version used : 4.10.0

Though I see two tabs ( 1. parent tab main page and 2. is the child tab) but when I switch to child through below code I don`t see any kind of exception thrown. But when I perform any operations like click on any button in child window it throws an exception "TypeError: JSON.stringify is not a function".

Below is the code snippet.

//Loop through until we find a new window handle

 for (String windowHandle : driver.getWindowHandles()) { if(!originalWindow.contentEquals(windowHandle)) { System.out.println("Child win : "+windowHandle); driver.switchTo().window(windowHandle); break; } } 

driver.findElement(By.name("aspnetForm")).click();

Can somebody please advise me here why i should be getting this error "Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Runtime.callFunctionOn threw exception: TypeError: JSON.stringify is not a function"

Consloe log below :

Child win : DEFC56C2E255CB00E7CF1C779B81E7EE Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Runtime.callFunctionOn threw exception: TypeError: JSON.stringify is not a function at buildError (:323:18) (Session info: chrome=114.0.5735.199) Build info: version: '4.10.0', revision: 'c14d967899' System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '11.0.18' Driver info: org.openqa.selenium.chrome.ChromeDriver Command: [748156ce82a3898c61c8bc461a5ecbbb, findElement {using=name, value=aspnetForm}] Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 114.0.5735.199, chrome: {chromedriverVersion: 114.0.5735.90 (386bc09e8f4f..., userDataDir: C:\Users\baluz\AppData\Loca...}, goog:chromeOptions: {debuggerAddress: localhost:58574}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: windows, proxy: Proxy(), se:cdp: ws://localhost:58574/devtoo..., se:cdpVersion: 114.0.5735.199, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:extension:minPinLength: true, webauthn:extension:prf: true, webauthn:virtualAuthenticators: true} Session ID: 748156ce82a3898c61c8bc461a5ecbbb at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490) at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:199) at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:132) at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:51) at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:191) at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:196) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:171) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:531) at org.openqa.selenium.remote.ElementLocation$ElementFinder$2.findElement(ElementLocation.java:165) at org.openqa.selenium.remote.ElementLocation.findElement(ElementLocation.java:66) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:350) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:344) at com.basePage.EX3.main(EX3.java:91)

2
  • Is the URL public, so we can access?CommentedJul 2, 2023 at 18:52
  • No. sorry this is internal site hence can`t be shared.CommentedJul 3, 2023 at 6:35

2 Answers 2

0

The site must have done something to the top-level JSON object. For example if a script on that page does this in global scope:

JSON = "{}" 

Then JSON.stringify will be undefined. Apparently Selenium is counting on it for passing things back and forth to Python

    0

    This is the issue with latest chrome browser.

    After few analysis just found three ways of doing things

    1. By using older chrome versions Step1: Uninstall Chrome Step2: Delete Chrome Data from below path %LOCALAPPDATA%\Google\Chrome\User Data Step3: Download Older Chrome version via FileHippo,Chromium Cypress, SlimJet. etc

    Steps to install older chrome browser versions

    Now you run your scripts with chromium browser it will work as expected.

    1. Case 2: Using javascript file Basically you should do is run a snippet of JS code to define the JSON.stringify method before you perform any find element or send key action, for my solution is using (JavascriptExecutor)webdrive.execute() to do this, the JS code you need just like following, also you can find this in JSON-js/json2.js

    save this javascript file and execute it using javascriptexecutor

    JavascriptExecutor obj = (JavascriptExecutor) driver;

    obj.executeScript("json2.js", args);

    1. Using Firefox browser i was able to run my automation scripts smoothly using below code which use to wait and then perform actions and it worked fine for me.

    new WebDriverWait(driver, 20).until(driver -> ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete"));

    The problem was with chrome browser latest version i hope it would be fixed in coming versions.

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.