As per the selenium documentation found at this link, the driver.executeScript() method is designed to return specific types of values:
- If the result is an HTML element, a WebElement is returned
- If the result is a decimal, a Double is returned
- If the result is a non-decimal number, a Long is returned
- If the result is a boolean, a Boolean is returned
- For all other scenarios, a String is returned.
- If the result is an array, a List is returned with each object following the aforementioned rules. Nested lists are supported as well.
- If the result is a map, a Map is returned with values following the specified rules.
If the value is null or there is no return value, then null is returned.
This means that the executeScript method cannot return window objects.
Instead of expecting a window object in return, it is recommended to encapsulate your JavaScript logic within the executeScript method and then return any suitable type of value as described above. This returned value can then be used for further operations in your code.