如何在 Chrome 中使用 Selenium 处理另存为对话框

2023-06-06Python开发问题
12

本文介绍了如何在 Chrome 中使用 Selenium 处理另存为对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试使用 Selenium Chrome 网络驱动程序下载文件,但我不知道如何处理另存为对话框.

I am trying to download a file using the Selenium Chrome web driver but I don't know how to deal with save as dialog box.

我已经看到很多关于如何使用 Firefox 执行此操作的答案,但没有使用 Chrome.

I have seen many answers on how to do this using Firefox but none using Chrome.

推荐答案

profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2) # 自定义地点profile.set_preference('browser.download.manager.showWhenStarting',假) profile.set_preference('browser.download.dir', '/tmp')profile.set_preference('browser.helperApps.neverAsk.saveToDisk','文本/csv')

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', '/tmp') profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/csv')

设置这些首选项后,浏览器不会显示弹出对话框询问您是否要下载保存或其他.什么时候可以只使用 find_some_eleme = driver.find_element_by_xpath('''<somexpath>''').click() 我们可以使用任何其他方法来定位元素 xpath/id/css/name...我们自由地使用方法 click() 因为不会有对话框.或 .setPreference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream,text/csv")

After setting these preferences the browser won't show up pop dialog asking for whether you want to download save or other. When can then just use find_some_eleme = driver.find_element_by_xpath('''<somexpath>''').click() we can use any other method of locating the element xpath/id/css/name... and we use the method click() freely because there won't be a dialog. or .setPreference("browser.helperApps.neverAsk.saveToDisk","application/octet-stream,text/csv")

对于 Chrome:

chromedriver = "path/to/chromedriver"

os.environ["webdriver.chrome.driver"] = chromedriver
chrome_options = Options()

# this is the preference we're passing
prefs = {'profile.default_content_setting_values.automatic_downloads': 1}
chrome_options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)

这篇关于如何在 Chrome 中使用 Selenium 处理另存为对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10

按10分钟间隔对 pandas 数据帧进行分组
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)...
2024-08-22 Python开发问题
11