使用 Selenium VBA - 如何以无头模式下载文件

发布于 2025-01-12 00:14:15 字数 1607 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笨死的猪 2025-01-19 00:14:15
  Dim bot As New ChromeDriver, post As Object
  With bot
    .SetPreference "download.default_directory", ThisWorkbook.Path & "\"
    .Timeouts.ImplicitWait = 1000
    .AddArgument "--headless"
    .Start
    .get "http://"
  end with
  Dim bot As New ChromeDriver, post As Object
  With bot
    .SetPreference "download.default_directory", ThisWorkbook.Path & "\"
    .Timeouts.ImplicitWait = 1000
    .AddArgument "--headless"
    .Start
    .get "http://"
  end with
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文