1

So I am working on a project where I have to download hundreds of report from web server of SSRS. I wrote a program in python using selenium. The requirement is that the reports should be downloaded in background. I tried to get to the URL which downloads the report in excel format using driver.get(url) and the file was downloaded.

Whenever I connect to the URL a window pops up asking for user id and password. enter image description here So to address this I added two lines of code in chrome_options. enter image description here

Then the popup disappeared and the report was downloaded normally. But as soon as I add chrome_options(--headless) in the code the file is not downloaded only a console opens and it closes after a whileenter image description here

So what I am saying is that this code works enter image description here

But this code does not enter image description here

I cannot understand why this is happening. Can anyone explain to me why my code does not work in headless mode? Here's my full code (URL altered for privacy)

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from subprocess import CREATE_NO_WINDOW
import time
import datetime
import os

service = Service(executable_path=r'C:\Program Files\chromedriver_win32\chromedriver.exe')
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless=new")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--auth-server-whitelist=*")
chrome_options.add_argument("--auth-negotiate-delegate-whitelist=*")
chrome_options.add_argument("--log-level=0")
service.creation_flags = CREATE_NO_WINDOW

print('starting download')
driver=webdriver.Chrome(service=service,options=chrome_options) 
driver.get("http://10.1.2.69:99/ReportServer")
time.sleep(3)
print('file downloaded')
driver.quit()
Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
aparajeya
  • 21
  • 4

2 Answers2

1

Instead of:

chrome_options.add_argument('--headless')

You need to use:

  • For chrome v96 till chrome v108:

    chrome_options.add_argument('--headless=chrome')
    
  • For chrome v109 and above:

    chrome_options.add_argument('--headless=new')
    

and execute your test.

See: DeprecationWarning: headless property is deprecated, instead use add_argument('--headless') or add_argument('--headless=new') on Selenium 4.8.0 Python


Youe effective code block will be:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
# chrome_options.add_argument('--headless=chrome') # for chrome v96 to chrome v108
chrome_options.add_argument('--headless=new') # for chrome v109 and above
driver = webdriver.Chrome(options=chrome_options)

References

A couple of helpful discussions:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1

See: https://stackoverflow.com/a/73840130/7058266 (Downloading with chrome headless and selenium)

The Chromium developers recently added a 2nd headless mode (in 2021). See https://bugs.chromium.org/p/chromium/issues/detail?id=706008#c36

They later renamed the option in 2023 for Chrome 109 -> https://github.com/chromium/chromium/commit/e9c516118e2e1923757ecb13e6d9fff36775d1f4

For Chrome 109 and above, the --headless=new flag will now allow you to get the full functionality of Chrome in the new headless mode, and you can even run extensions in it. (For Chrome versions 96 through 108, use --headless=chrome)

Usage: (Chrome 109 and above):

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

Usage: (Chrome 96 through Chrome 108):

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

If something works in regular Chrome, it should now work with the newer headless mode too.

Michael Mintz
  • 9,007
  • 6
  • 31
  • 48