The browser blocks my download when I use headless [Selenium]

Question:

I am trying to download a file using automation with Selenium, but Google Chrome blocks the download when I request the download file. I read that I need user_agent, but don’t work in my code.

def getting_file(URL):

    page = Options()
    page.headless = False
    service = Service(ChromeDriverManager().install())
    driver = webdriver.Chrome(service=service, options=page)
    driver.get(url)
    driver.get("https://www.rtm.selic.gov.br/extrato-selic-web/pesquisa-extrato-diario-asel.jsp?bust=1662995259495")
    time.sleep(5)
    driver.find_element(By.CSS_SELECTOR, "tr:nth-child(2) .linkAcao").click()
    time.sleep(5)
    driver.close()

Answers:

Regular headless mode in Chrome has restrictions. But there’s a workaround:

The Chromium developers recently added a 2nd headless mode that functions the same way as normal Chrome.

--headless=chrome

(the old way was: --headless or options.headless = True)

The New Headless Mode Usage:

options.add_argument("--headless=chrome")

You should be able to use that mode and get the same results as headed mode.

In your code, change the following lines:

    page = Options()
    page.headless = False

to this:

    page = Options()
    page.add_argument("--headless=chrome")

and that should give you a better headless mode for your situation.

There’s more info on that here: https://bugs.chromium.org/p/chromium/issues/detail?id=706008#c36


January 2023 UPDATE:

Starting with Chrome 109, the Chromium team renamed the option to --headless=new. See https://github.com/chromium/chromium/commit/e9c516118e2e1923757ecb13e6d9fff36775d1f4 for details.

Answered By: Michael Mintz