使用 Selenium VBA - 如何以无头模式下载文件
我在 java 脚本和 python 中找到了有关如何使用 chrome 驱动程序以无头模式下载文件的代码片段。但是,我不确定如何复制代码行以使用 VBA 运行(我特别使用 Excel),
这里是我发现不起作用的设置,
driver.SetPreference "download.prompt_for_download", False
driver.SetPreference "download.directory_upgrade", True
driver.SetPreference "download.default_directory", FLDR_NAME
driver.SetPreference "safebrowsing.enabled", False
driver.SetPreference "download.prompt_for_download", False
driver.SetPreference "download.setdownloadbehaviour", "allow"
driver.SetPreference "safebrowsing.disable_download_protection", True
This was an attempt at copying the javascript version of downloading in headless mode below
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("prefs", {
"download.default_directory": "/path/to/download/dir",
"download.prompt_for_download": False,
})
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': "/path/to/download/dir"}}
command_result = driver.execute("send_command", params)
这就是我的基本代码的样子:
Dim bot As New ChromeDriver, post As Object
With bot
.Timeouts.ImplicitWait = 1000
.AddArgument "--headless"
.Start
.get "http://"
当我的自动化在无头模式中运行时模式,到达下载点。它成功单击了下载链接,但下载并未开始。没有错误,屏幕截图显示没有发生任何其他事情。当我在没有无头模式的情况下运行完全相同的代码时,它工作得很好。
I have found code snippets in java script and in python on how to enable downloading of files in headless mode using the chrome driver. However I am unsure on how to replicate the lines of code to run using VBA (Im using excel in particular)
here are settings that I have found that don't work
driver.SetPreference "download.prompt_for_download", False
driver.SetPreference "download.directory_upgrade", True
driver.SetPreference "download.default_directory", FLDR_NAME
driver.SetPreference "safebrowsing.enabled", False
driver.SetPreference "download.prompt_for_download", False
driver.SetPreference "download.setdownloadbehaviour", "allow"
driver.SetPreference "safebrowsing.disable_download_protection", True
This was an attempt at copying the javascript version of downloading in headless mode below
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_experimental_option("prefs", {
"download.default_directory": "/path/to/download/dir",
"download.prompt_for_download": False,
})
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': "/path/to/download/dir"}}
command_result = driver.execute("send_command", params)
this is what my base code looks like:
Dim bot As New ChromeDriver, post As Object
With bot
.Timeouts.ImplicitWait = 1000
.AddArgument "--headless"
.Start
.get "http://"
When my automation runs in headless mode, gets to the point of downloading. It successfully clicks the download link but the download does not commence. There is no error and the screenshot shows nothing else has happened. When I run the exact same code without headless mode it works perfectly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)