Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

376
Views
executeScript() and executeAsyncScript() of javascriptExecutor interface is not behaving as expected

I know that executeScript() function of JavascriptExecutor interface in selenium requires no signalling mechanism and it executes javascript inside it as synchronous(single threaded).

While executeAsyncScript() function of JavascriptExecutor interface requires signalling mechanism in the form of callback i.e. arguments[arguments.length-1] and it executes javascript inside it as asynchronously(multithreaded) without blocking main selenium code.

This means that if there is sleep in executeScript() javascript code then it will wait for that sleep time then execute further selenium statements. While if there is sleep in executeAsyncScript(), it will execute main selenium code in parallel to that sleep without blocking main code.

Now consider following code:

public static void main(String args[]) {
        
        WebDriverManager.chromedriver().setup();
        
        WebDriver driver=new ChromeDriver();
        JavascriptExecutor js=(JavascriptExecutor)driver;
        
    
        System.out.println("1");
        String javascript="var callback=arguments[arguments.length-1]; window.setTimeout(callback,15000);";  //callback has no problem here. Statement:1
        js.executeScript(javascript);  //Statement:2
        
        System.out.println("2");
        driver.quit();
    }

Now when I execute this code it does not wait for 15 sec sleep and immediately prints 2 and quit the browser while ideally it should behave synchronously i.e. it should block the main thread for 15 sec and after that it should print the 2 and quit the browser.

Now if I replace the statements 1 and 2 in above code with:

String javascript="var callback=arguments[arguments.length-1]; window.setTimeout(callback,15000);";
js.executeAsyncScript(javascript);

as:

public static void main(String args[]) {
        
        WebDriverManager.chromedriver().setup();
        
        WebDriver driver=new ChromeDriver();
        JavascriptExecutor js=(JavascriptExecutor)driver;
        
    
        System.out.println("1");
    
    String javascript="var callback=arguments[arguments.length-1]; window.setTimeout(callback,15000);";
    
    js.executeAsyncScript(javascript);
    
    System.out.println("2");
    
    driver.quit();
    }

If I run above code it waits for 15 sec sleep and print 2 after 15 sec and quit the browser after printing 2 after that 15 sec sleep.

Why they are behaving oppositely?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!