title: Understanding execute async script in Selenium
body: <p>I've been using <code>selenium</code> (with <a href="http://selenium-python.readthedocs.org/" rel="noreferrer">python bindings</a> and through <a href="http://angular.github.io/protractor/#/" rel="noreferrer"><code>protractor</code></a> mostly) for a rather long time and every time I needed to execute a javascript code, I've used <code>execute_script()</code> method. For example, <a href="http://selenium-python.readthedocs.org/en/latest/faq.html#how-to-scroll-down-to-the-bottom-of-a-page" rel="noreferrer">for scrolling the page</a> (python):</p>
  
  <pre><code>driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
  </code></pre>
  
  <p>Or, for <a href="https://stackoverflow.com/questions/27794316/protractor-scrolling-a-table-and-testing-for-infinite-scroll">infinite scrolling inside an another element</a> (protractor):</p>
  
  <pre><code>var div = element(by.css('div.table-scroll'));
  var lastRow = element(by.css('table#myid tr:last-of-type'));
  
  browser.executeScript("return arguments[0].offsetTop;", lastRow.getWebElement()).then(function (offset) {
      browser.executeScript('arguments[0].scrollTop = arguments[1];', div.getWebElement(), offset).then(function() {
          // assertions
  
      });
  });
  </code></pre>
  
  <p>Or, for getting a <a href="https://stackoverflow.com/questions/27307131/selenium-webdriver-how-do-i-find-all-of-an-elements-attributes">dictionary of all element attributes</a> (python):</p>
  
  <pre><code>driver.execute_script('var items = {}; for (index = 0; index &lt; arguments[0].attributes.length; ++index) { items[arguments[0].attributes[index].name] = arguments[0].attributes[index].value }; return items;', element)
  </code></pre>
  
  <hr>
  
  <p>But, WebDriver API also has <a href="http://selenium-python.readthedocs.org/en/latest/api.html#selenium.webdriver.remote.webdriver.WebDriver.execute_async_script" rel="noreferrer"><code>execute_async_script()</code></a> which I haven't personally used.</p>
  
  <p>What use cases does it cover? When should I use <code>execute_async_script()</code> instead of the regular <code>execute_script()</code>?</p>
  
  <p><sup>The question is selenium-specific, but language-agnostic.</sup></p>