Python Headless Chrome Slows Tests by 20x

Viewed 35

I have written a test program that performs tests on websites. I am trying to run my tests in headless Chrome using the --headless argument. While it works fine in a normal browser window, it runs 20 times slower in headless mode.Phantomjs support removed; Are there different alternatives you can suggest for Windows, chrome headless is unfortunately very unstable.

These are the arguments I use

chrome_options = webdriver.ChromeOptions()

chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-popup-blocking")
chrome_options.add_argument("--profile-directory=Default")
chrome_options.add_argument("--ignore-certificate-errors")

chrome_options.add_argument('--disable-features=TranslateUI')
chrome_options.add_argument('--disable-translate')
chrome_options.add_argument("--user-agent=User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36")
chrome_options.add_argument('--headless')
chrome_options.add_argument("--disable-blink-features=AutomationControlled")
chrome_options.add_argument("--proxy-server='direct://'")
chrome_options.add_argument("--proxy-bypass-list=*")
chrome_options.add_argument("--blink-settings=imagesEnabled=false")
chrome_options.add_argument("--window-size=1366,728")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--disable-browser-side-navigation")
chrome_options.add_argument("--no-sandbox")
1 Answers

Some of your options args don't look correct. You can try using https://github.com/seleniumbase/SeleniumBase for your Python Selenium tests. It sets optimal args for headless mode, and any other mode that you run in. SeleniumBase tests are run with pytest. For Headless Mode, use pytest --headless, and your headless SeleniumBase tests should much faster than your standard GUI tests.

Related