Im building a function to scroll on a website for a given amount of time using the executeScript function in selenium webdriver in javascript. the default timeout for a script appears to be around 15 seconds until it throws an error (ScriptTimeoutError). I have found lots of documentation on how to modify the timeout length in other languages like Selenium for Java but nothing for the JS library. The documentation for selenium JS is awful.
The following test code to reproduce the error. at around 25 seconds it will throw a ScriptTimeoutError
const { Builder, By, Key } = require("selenium-webdriver");
async function example() {
let driver = await new Builder().forBrowser("chrome").build();
await driver.get("https://google.com");
await driver.executeScript(`
await new Promise((resolve) => setTimeout(resolve, 30000));
`);
console.log("DONE!");
}
example();
“
here is the stack:
ScriptTimeoutError: script timeout
(Session info: chrome=118.0.5993.118)
at Object.throwDecodedError (c:UsersAdminDocumentsselenium scriptsseleniumDemonode_modulesselenium-webdriverliberror.js:524:15)
at parseHttpResponse (c:UsersAdminDocumentsselenium scriptsseleniumDemonode_modulesselenium-webdriverlibhttp.js:601:13)
at Executor.execute (c:UsersAdminDocumentsselenium scriptsseleniumDemonode_modulesselenium-webdriverlibhttp.js:529:28)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Driver.execute (c:UsersAdminDocumentsselenium scriptsseleniumDemonode_modulesselenium-webdriverlibwebdriver.js:745:17)
at async example (c:UsersAdminDocumentsselenium scriptsseleniumDemoteststest 4.js:8:3) {
remoteStacktrace: 'tGetHandleVerifier [0x00007FF705638EF2+54786]n' +
2
Answers
After several hours of research i found the solution by accident. there is a function in webDriver called manage(). this function allows you to call several useful functions to control the selenium server. one of the functions is setTimeout() takes in an object that sets the different timeouts for the webdriver (script, pageLoad, implicit). an await statement must be used to set the timeout therefore it must be done in an async function.
the setTimeout function takes in a javascript object.
here are some other useful functions available in the manage class:
final solution:
I think you should be executing your script as async (
driver.executeAsyncScript()
), given you need it to await for the promise resolution. You could then usedriver.manage().timeouts().setScriptTimeout()
if your timeout threshold is still low.Note setScriptTimeout() will only affect async scripts.