Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download file with firefox bypassing popup : Selenium Python

I am using selenium with python to download certain files from this web page. I have been previously using set preferences to create Firefox profile, and they worked perfectly fine. In this case the download pop up opens on same page, despite given preferences it always opens pop up for action (save/open). can anybody help bypassing it and download file automatically without pop up alert ?

The preference settings are:

fp = webdriver.FirefoxProfile()
        fp.set_preference("browser.download.folderList", 2)
        fp.set_preference("browser.download.manager.showWhenStarting", False)
        fp.set_preference("browser.download.dir", downloadDir)
        fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv")


        self.driver = webdriver.Firefox(firefox_profile=fp)

File i am trying to download: File to be downloaded

Thank you, beside if full code is required is mentioned below:

# -*- coding utf-8 -*-
from selenium.webdriver.firefox.options import Options
from selenium import webdriver
import time
import os
import shutil
import uuid

class crawlOcean():

    def __init__(self):
        print("hurray33")
        global downloadDir
        downloadDir = ""

        fp = webdriver.FirefoxProfile()
        fp.set_preference("browser.download.folderList", 2)
        fp.set_preference("browser.download.manager.showWhenStarting", False)
        fp.set_preference("browser.download.dir", downloadDir)
        fp.set_preference("browser.helperApps.neverAsk.saveToDisk",
                          "text/plain, application/octet-stream, application/binary, text/csv, application/csv, application/excel, text/comma-separated-values, text/xml, application/xml")
        fp.set_preference("pdfjs.disabled", True)
        options = Options()
        options.add_argument("--headless")
        self.driver = webdriver.Firefox(firefox_profile=fp)
        #self.driver = webdriver.Firefox()
        print("hurray")
        self.driver.implicitly_wait(15)
        self.driver.get("http://www.oceanenergyireland.com/testfacility/corkharbour/observations")
        self.verificationErrors = []
        self.accept_next_alert = True

    def crawl(self):
        print("see")
        driver = self.driver
        driver.execute_script("window.scrollTo(0, 600)")
        index = 0
        driver.switch_to.frame(index)
        driver.find_element_by_xpath("//div[@id='CorkTideHeight']/div[3]/button[2]").click()
        time.sleep(3)
        driver.find_element_by_xpath("//div[@id='CorkTideHeight']/div[3]/div/ul/li[5]").click()
        time.sleep(5)

if __name__ == '__main__':
    obj = crawlOcean()
    obj.crawl()
like image 777
Northern Shadow Avatar asked Oct 20 '18 15:10

Northern Shadow


1 Answers

Instead of text/csv. Try attachment/csv, it is working. Actually this download file is generated in javascript itself and they have set custom data type like this.

    fp = webdriver.FirefoxProfile()
    fp.set_preference("browser.download.folderList", 2)
    fp.set_preference("browser.download.manager.showWhenStarting", False)
    fp.set_preference("browser.download.dir", downloadDir)
    fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "attachment/csv")
    self.driver = webdriver.Firefox(firefox_profile=fp)
like image 169
Navarasu Avatar answered Oct 13 '22 17:10

Navarasu