0

I am trying to save excel file (The file is in .xls format) using selenium in python. I have used all the MIME types for .xls format. But after setting the profile right, it is still showing the dialogue box. This is the code of the profile.

profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2) # custom location
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.download.dir', 'F://')
profile.set_preference("browser.helperApps.alwaysAsk.force", False)
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/msexcel, application/x-msexcel,application/x-ms-excel,application/x-excel,application/x-dos_ms_excel,application/xls,application/x-xls')


driver = webdriver.Firefox(profile)
driver.get("http://agmarknet.gov.in/SearchCmmMkt.aspx?Tx_Commodity=1&Tx_State=0&Tx_District=0&Tx_Market=226&DateFrom=01-Jan-2009&DateTo=15-Aug-2018&Fr_Date=01-Jan-2009&To_Date=15-Aug-2018&Tx_Trend=0&Tx_CommodityHead=Wheat&Tx_StateHead=--Select--&Tx_DistrictHead=--Select--&Tx_MarketHead=Amritsar")
driver.find_element_by_id("cphBody_ButtonExcel").click()

How to automatically download the file and get rid of the dialogue box? I have referred to different answers on stack overflow, but nothing helped.

  • Did you try all MIME-types mentioned in [this question](https://stackoverflow.com/questions/974079/setting-mime-type-for-excel-document)? – Andersson Aug 15 '18 at 14:04
  • Yes, I tried all the MIME-types mentioned in this link. Even tried following the brute force method mentioned in this link.(https://stackoverflow.com/questions/13839544/using-selenium-webdrivers-method-browser-helperapps-neverask-savetodisk-how-ca/34780823) Is there anything else I can do? – Subham Agarwala Aug 15 '18 at 14:42
  • It is using "Content-Type: application/vnd.xls". I found that by using the Developer tools (press F12) in Firefox, going to the "Network" pane, clicking on the "Export To Excel" button on the web page, then looking at the "Raw headers". – Andrew Morton Aug 17 '18 at 09:44
  • @Andersson Based on this [meta discussion](https://meta.stackoverflow.com/questions/372613/am-i-justified-to-answer-a-question-which-i-have-closed-as-a-duplicate-earlier-b) can we ask your rational perspective why the question is not a duplicate of [Python: Unable to download with selenium in webpage](https://stackoverflow.com/questions/44072022/python-unable-to-download-with-selenium-in-webpage) the reason behind reopening this question? – undetected Selenium Aug 18 '18 at 07:51
  • @DebanjanB , maybe because OP has already tried the solution marked as *"Accepted"*? – Andersson Aug 18 '18 at 08:00
  • @Andersson Taking a decision based on **maybe** factors is pure _speculation_ as per stackoverflow standards. – undetected Selenium Aug 18 '18 at 08:07
  • @DebanjanB , yeah. Good point. Let's stick to stackoverflow standards – Andersson Aug 18 '18 at 08:15
  • @Andersson Sounds great !!! The bottom-line, moving forward as a _dupe hammer reopener: please ask why a question was closed as dup or if there is a better one instead of instantly reopening_. Thanks – undetected Selenium Aug 18 '18 at 08:20
  • @AndrewMorton thanks for the solution. It has worked finally. – Subham Agarwala Aug 18 '18 at 15:00

0 Answers0