Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Waiting for JavaScript event with Selenium

I am building an automation framework on top of Selenium (Node.js) consisting on a number of steps. Each step follows the previous one, after it completes, returning a promise (like the one returned by Selenium's driver.click(), etc). Is it possible to wait for a JavaScript event to trigger on the browser? If so, what is the pattern to follow?

like image 868
Ricardo Peres Avatar asked Oct 17 '25 20:10

Ricardo Peres


1 Answers

Use .executeAsyncScript to wait for an event to occur :

driver.executeAsyncScript(function(callback) {
  window.addEventListener('message', function onmessage() {
    window.removeEventListener('message', onmessage);
    callback();
  });
});

The doc:

http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/webdriver_exports_WebDriver.html#executeAsyncScript

like image 168
Florent B. Avatar answered Oct 20 '25 10:10

Florent B.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!