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

Using Selenium VBA - How to Download files in headless mode

我在 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://"

当我的自动化 运行s 处于无头模式时,到达下载点。它成功地点击了下载 link 但下载没有开始。没有错误,屏幕截图显示没有发生任何其他事情。当我 运行 没有无头模式的完全相同的代码时,它工作得很好。

  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