問題描述
我正在嘗試使用 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 處理另存為對話框的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!