Selenium Chromedriver下载配置文件

时间:2014-01-13 01:14:01

标签: python google-chrome selenium selenium-webdriver selenium-chromedriver

我通常使用以下函数为Firefox设置配置文件

def FirefoxProfile(path, handlers):
        debug = True
        if debug: print "%r - %s(%r, %r)" % (time.asctime(), "FirefoxProfile", path, handlers)
        profile = webdriver.FirefoxProfile()
        profile.set_preference("browser.download.manager.showWhenStarting",False)
        if (not(isfile(path)) & exists(path)):
            profile.set_preference("browser.download.folderList",2)
            profile.set_preference("browser.download.dir", path)
            profile.set_preference("browser.download.downloadDir", path)
            profile.set_preference("browser.download.defaultFolder", path)
        else:
            profile.set_preference("browser.download.folderList",1)
        profile.set_preference("browser.helperApps.alwaysAsk.force", False)
        profile.set_preference("browser.helperApps.neverAsk.saveToDisk", handlers)
        profile.set_preference("pdfjs.disabled", True)
        profile.update_preferences()

        if debug: print "done. - %r" % time.asctime()
        return profile

# Somewhere else 
self.profile = PyWebBot.FirefoxProfile(config['downloads'], config['handlers'])
self.driver = webdriver.Firefox(self.profile)

如何为Chrome执行此操作?我需要相同的设置

  1. 将文件保存到自定义目录
  2. 自动保存(永不问)

  3. 修改

    def Google_desired_capabilities(path=os.getcwd()):
            return {"prefs": {
                                "download.default_directory": path,
                                "download.prompt_for_download": False
                            },
                    "switches": ["-silent", "--disable-logging"],
                    "chromeOptions": {
                                "args": ["-silent", "--disable-logging"]
                            }
                    }
    

    更新

        chrome_options = webdriver.ChromeOptions()
        chrome_options.prefs = PyWebBot.Google_desired_capabilities(config['downloads'])['prefs']
        self.driver = webdriver.Chrome(path.join(config['drivers'],'chromedriver.exe'), chrome_options=chrome_options)
    

0 个答案:

没有答案