Bird
0
0

Which of the following is the correct syntax to execute an async script in Selenium Java?

easy📝 Syntax Q12 of 15
Selenium Java - JavaScriptExecutor
Which of the following is the correct syntax to execute an async script in Selenium Java?
Adriver.executeAsyncScript("var callback = arguments[0]; setTimeout(() => callback(10), 1000);");
Bdriver.executeAsyncScript("return 5 + 5;");
Cdriver.executeScript("var callback = arguments[0]; callback(10);");
Ddriver.executeAsyncScript(() -> { return 10; });
Step-by-Step Solution
Solution:
  1. Step 1: Identify async script syntax

    Async scripts require a callback function as the last argument to signal completion.
  2. Step 2: Check each option

    driver.executeAsyncScript("var callback = arguments[0]; setTimeout(() => callback(10), 1000);"); correctly defines a callback and calls it after 1 second delay. driver.executeAsyncScript("return 5 + 5;"); lacks callback, C uses executeScript (sync), D uses wrong syntax.
  3. Final Answer:

    driver.executeAsyncScript("var callback = arguments[0]; setTimeout(() => callback(10), 1000);"); -> Option A
  4. Quick Check:

    Callback with setTimeout = A [OK]
Quick Trick: Async scripts need callback as last argument [OK]
Common Mistakes:
  • Not passing or calling the callback function
  • Using executeScript instead of executeAsyncScript
  • Wrong Java syntax for async scripts

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes