title: Downloading Excel File Using Python Selenium
body: <p>i created simple python for downloading excel file from internet, I'm using python and selenium with Chromedriver, the problem is, when download complete and file is saved, i can't read that excel file (tried with Libreoffice, MsExcel), but it can be readed when I manually download that file whitout Selenium, when i tried to read those file using <code>python xlrd</code> the error is <code>zipfile.BadZipFile: Bad magic number for file header</code></p>
  
  <p>at first i was think the download is not finished yet because closing the browser too soon, tried to increasing <code>sleep(20</code> but the result is the same.</p>
  
  <p>is there anything I have missed in the process that I have been working on?</p>
  
  <p>here is my python script.</p>
  
  <pre><code>import time
  from selenium import webdriver
  from selenium.webdriver.common.by import By
  from selenium.webdriver.support.ui import WebDriverWait
  from selenium.webdriver.support import expected_conditions as EC
  from selenium.common.exceptions import TimeoutException
  
  def every_downloads_chrome(driver):
      if not driver.current_url.startswith("chrome://downloads"):
          driver.get("chrome://downloads/")
      return driver.execute_script("""
          var items = downloads.Manager.get().items_;
          if (items.every(e =&gt; e.state === "COMPLETE"))
              return items.map(e =&gt; e.fileUrl || e.file_url);
          """)
  
  uri = "https://cfs.ojk.go.id/cfs/ReportViewerForm.aspx?BankCode=PT.+BPR+Cikarang+Raharja&amp;Month=3&amp;Year=2019&amp;FinancialReportPeriodTypeCode=R&amp;FinancialReportTypeCode=BPK-900-000002"
  option = webdriver.ChromeOptions()
  option.add_argument("--incognito")
  option.add_argument("--window-size=400,400")
  option.add_argument('disable-component-cloud-policy')
  option.add_experimental_option("prefs", {
    "download.prompt_for_download": False,
    "download.directory_upgrade": False,
    "safebrowsing.enabled": True
  })
  browser = webdriver.Chrome(executable_path=chromedriver_path, options=option)
  browser.get(uri)
  timeout = 20
  try:
      WebDriverWait(browser, timeout).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="CFSReportViewer_ctl05_ctl04_ctl00_ButtonImg"]')))
  except TimeoutException:
      browser.quit()
  
  #Download the file by execute JS Command
  browser.execute_script("$find('CFSReportViewer').exportReport('EXCELOPENXML');")
  
  #wait until download is finish
  WebDriverWait(browser, 120,1).until(every_downloads_chrome)
  
  #break 2 second and quit
  time.sleep(2)
  browser.quit()
  </code></pre>
  
  <p>I'm trying to download excel from this site</p>
  
  <pre><code>https://cfs.ojk.go.id/cfs/ReportViewerForm.aspx?BankCode=PT.+BPR+Cikarang+Raharja&amp;Month=3&amp;Year=2019&amp;FinancialReportPeriodTypeCode=R&amp;FinancialReportTypeCode=BPK-900-000002
  </code></pre>
  
  <p>Btw I'm using Mac OS with Chrome <code>77</code> and Chromedrive <code>77.0.3865.40</code></p>
  
  <p>here is the case video
  <a href="https://drive.google.com/file/d/1N6q66AVpo4XtrZemxoD5E94xUohzcaNx/view" rel="nofollow noreferrer">https://drive.google.com/file/d/1N6q66AVpo4XtrZemxoD5E94xUohzcaNx/view</a></p>
  
  <h3>Update</h3>
  
  <p>It was my environment, I'm using virtualenv ro run this script and got that error message for downloaded file, but when i try to run whitout virtualenv the file can be read without a singel error, that's make me sure that was my virtualenv.</p>