Home > Software engineering >  Using Selenium VBA - How to Download files in headless mode
Using Selenium VBA - How to Download files in headless mode

Time:03-08

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.

CodePudding user response:

  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
  • Related