skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.

    await driver.manage().setTimeouts({ script: 60000 });
    

    the setTimeout function takes in a javascript object.

      {
             implicit: time-in-millisecconds,
             pageLoad: time-in-millisecconds,
             script: time-in-millisecconds
      }
    

    here are some other useful functions available in the manage class:

    final solution:

    let webdriver = require("selenium-webdriver");
    
    async function example() {
      // create driver
      let driver = await new webdriver.Builder().forBrowser("chrome").build();
    
      //   this function will set the script timeout to 60 secconds 
      //   you can also set other timeouts with this function it takes an object
      //   {
      //       implicit: TimeInMS,
      //       pageLoad: timeInMS,
      //       script: timeInMS
      //    }
      await driver.manage().setTimeouts({ script: 60000 });
    
      // this function will get the different timeouts for the selenium server
      //   { implicit: 0, pageLoad: 300000, script: 35000 }
      const timeouts = await driver.manage().
      console.log(timeouts);
    
      //   go to website
      await driver.get("https://bing.com");
    
      //   driver executes script for 35 secconds
      await driver.executeScript(`
      
        await new Promise((resolve) => setTimeout(resolve, 35000));
      
      `);
    
      console.log("DONE!");
    }
    
    example();
    

  2. 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 use driver.manage().timeouts().setScriptTimeout() if your timeout threshold is still low.

    Note setScriptTimeout() will only affect async scripts.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search