python - 当我已经在 Python 中设置 Saves 时,为什么 Selenium 仍然要求我配置 Saves?

标签 python selenium

我并不是真正的 Python 用户,但我正在使用一些从网上获取的代码来下载文件。其中代码之一是:

        urlpage = 'https://www150.statcan.gc.ca/n1/tbl/csv/' + '10100127' + '-eng.zip'
        profile = webdriver.FirefoxProfile()
        profile.set_preference("browser.download.folderList", 2)
        profile.set_preference("browser.download.manager.showWhenStarting", False)
        profile.set_preference("browser.download.dir", 'D:\downloads')
        profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")
        driver = webdriver.Firefox()
        driver.get(urlpage)

据我所知,应该只是将文件下载到我的 D: 驱动器的下载文件夹中,但是当我运行代码时,网页会打开,然后询问我是否要查看或下载该文件。代码有什么问题吗?还是我做错了什么?

不确定这是否是重要信息,但我正在使用 PyCharm 作为我的 IDE

最佳答案

这是您应该使用的脚本,这会将文件保存在系统默认下载文件夹中。

FF_options = webdriver.FirefoxProfile()
FF_options.set_preference("browser.helperApps.neverAsk.saveToDisk","application/zip")
driver= webdriver.Firefox(firefox_profile=FF_options)

如果您想将下载的文件保存在特定位置,请添加以下首选项。

# change the path here, current line will save in the working directory meaning 
# the location where your script is.
FF_options.set_preference("browser.download.dir", os.getcwd()) 
FF_options.set_preference("browser.download.folderList",2)

关于python - 当我已经在 Python 中设置 Saves 时,为什么 Selenium 仍然要求我配置 Saves?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56991061/

相关文章:

java - 测试通过,但操作没有发生 selenium webdriver + testng

python - 查找列中字符串的精确正则表达式匹配

Python:单独的实用程序文件或使用静态方法?

python - 列表的所有可能分割

python - 如何使用 python selenium 包打开 chrome webdriver,登录默认用户?

java - 如何使用 Selenium java 接口(interface)从网站上传文件

python ppt在图表中查找和替换

python - 如何制作一个 Iterable 函数(或者无论如何调用它)?

python - 如何使用 Selenium 向 ChromeDriver 提交 HTTP 身份验证(Flask BASIC Auth)

selenium - Chrome驱动程序的页面加载策略(更新至Selenium v​​3.12.0)