0 votes
1 view
in Devops and Agile by (20k points)

I'm using python-selenium and Chrome 59 and trying to automate a simple download sequence. When I launch the browser normally, the download works, but when I do so in headless mode, the download doesn't work.

# Headless implementation

from selenium import webdriver

chromeOptions = webdriver.ChromeOptions()

chromeOptions.add_argument("headless")

driver = webdriver.Chrome(chrome_options=chromeOptions)

driver.get('https://www.mockaroo.com/')

driver.find_element_by_id('download').click()

# ^^^ Download doesn't start

# Normal Mode

from selenium import webdriver

driver = webdriver.Chrome()

driver.get('https://www.mockaroo.com/')

driver.find_element_by_id('download').click()

# ^^^ Download works normally

I've even tried adding a default path:

prefs = {"download.default_directory" : "/Users/Chetan/Desktop/"}

chromeOptions.add_argument("headless")

chromeOptions.add_experimental_option("prefs",prefs)

Adding a default path works in the normal implementation, but the same problem persists in the headless version.

How do I get the download to start in headless mode?

1 Answer

0 votes
by (28.4k points)

This feature of Chrome prevents from software to download files to your computer. In order to enable it via DevTools do this: 

async function setDownload () {

  const client = await CDP({tab: 'ws://localhost:9222/devtools/browser'});

  const info =  await client.send('Browser.setDownloadBehavior', {behavior : "allow", downloadPath: "/tmp/"});

  await client.close();

}

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...