title: "TransitionRejection" trying to download a file with a new element from JS using selenium
body: <p>I want to automate the download from a lot of files of a website.
  In order to do it I iterate through many pages getting the id from the files. I do this using selenium and a perl script.</p>
  
  <p>As there is no functionality to directly download a file in selenium, and I can not use some kind of curl because I need the session from selenium I'm trying to use the following workaround which I had already used in other projects.</p>
  
  <p>I create a JS element and execute a script with the url for each file, for example:</p>
  
  <pre><code>var a = document.createElement("a");
  a.setAttribute("href", "https://myurl.com/id1/export?format=TCX");
  a.setAttribute("download", "https://myurl.com/id1/export?format=TCX");
  document.body.appendChild(a);
  a.click();
  document.body.removeChild(a);
  </code></pre>
  
  <p>But when I try to do this I become the following error in the script:</p>
  
  <blockquote>
    <p>Error while executing command: stale element reference: The element
    reference of 
    is stale; either the element is no longer attached to the DOM, it is
    not in the current frame context, or the document has been refreshed
    at /Library/Perl/5.18/Selenium/Remote/Driver.pm line 391.  at
    /Library/Perl/5.18/Selenium/Remote/Driver.pm line 348.</p>
  </blockquote>
  
  <p>If I reproduce it directly in the firefox console I get this:</p>
  
  <blockquote>
    <p>TransitionRejection(type: 2, message: The transition has been
    superseded by a different transition, detail: Transition#2(
    'top.ids.info.byId'{"myId1":"XXXXX","myId2":"YYYYYY"} ->
    'home'{"feedId":null,"tag":null} ))</p>
  </blockquote>
  
  <p>If I just access the url with a normal GET from the browser I get the download prompt without problems. (As long as I'm logged in)</p>
  
  <p>If I get the url with selenium it works for the first url, but then it stays stuck there and does not download the second file.</p>
  
  <pre><code>  $driver-&gt;get($download_url1); # This one is downloaded
    $driver-&gt;get($download_url2); # This line is not executed
  </code></pre>