title: python selenium . how to switch back to previous window/tab after a download opens up a new tab
body: <p>The problem is when I download a file through a click(), it opens a new tab/window to initiate the download, then the browser automatically closes the tab/window. However, when I want to access the previous page in which the download link was, I get the error &quot;invalid session id&quot;.</p>
  <p>I get the error &quot;<strong>No such window exception</strong>&quot; when using Safari for the automation.</p>
  <p>If anyone knows how to deal with this issue I would appreciate all the help I could get. thank you all!</p>
  <p>my code is below and the error comes after trying to click <code>file_dl2</code></p>
  <pre><code>attachments = browser.find_element_by_id('sAttachments')
  attachments.click() 
  
  time.sleep(2)
  
  files = browser.find_element_by_xpath('//*[@id=&quot;FileListFI&quot;]/div[1]') 
  files.click()
  
  file_dl = browser.find_element_by_xpath('//*[@id=&quot;ctl00_chpDialog_hrefFileLink&quot;]/img')  
  file_dl.click()
  
  browser.implicitly_wait(10)
  
  file_dl2 = browser.find_element_by_xpath('//*[@id=&quot;ctl01_chpDialog_hrefFileLink&quot;]/img') 
  file_dl2.click()
  </code></pre>