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

Time:09-29

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()

CodePudding user response:

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

  • Related