Bird
0
0

How can you use executeAsyncScript in Selenium Java to wait up to 5 seconds for an element with id 'target' to appear on the page?

hard📝 Application Q8 of 15
Selenium Java - JavaScriptExecutor
How can you use executeAsyncScript in Selenium Java to wait up to 5 seconds for an element with id 'target' to appear on the page?
AUse Selenium's implicit wait instead of <code>executeAsyncScript</code>.
BCall <code>executeAsyncScript</code> once with a 5-second <code>setTimeout</code> and then check for the element.
CUse <code>executeScript</code> with a 5-second delay and then return the element.
DUse a loop inside the async script that checks for the element every 500ms and calls callback when found or after 5 seconds.
Step-by-Step Solution
Solution:
  1. Step 1: Understand async script waiting

    Async script can poll for element presence and call callback when found.
  2. Step 2: Implement polling with timeout

    Loop checks every 500ms up to 5 seconds, then calls callback with result.
  3. Final Answer:

    Use a loop inside the async script that checks for the element every 500ms and calls callback when found or after 5 seconds. -> Option D
  4. Quick Check:

    Polling inside async script waits for element [OK]
Quick Trick: Poll inside async script and call callback when ready [OK]
Common Mistakes:
  • Using executeScript which is synchronous
  • Not calling callback after timeout
  • Relying only on implicit waits for async scripts

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Java Quizzes