Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.4k 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 (27.5k 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();

}

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s Selenium training! 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...