Speed Up Selenium Python Headless Chrome on macOS
In this article
Running Selenium with headless Chrome on macOS can feel sluggish, especially when you have many test cases or scraping tasks. This guide shows you concrete, practical ways to cut down execution time—from browser options to code-level optimizations—without sacrificing reliability. You'll need Python 3, Selenium installed (pip install selenium), and Chrome installed on your Mac. No other tools are required.
What You'll Need
- Python 3.6+ installed
- Selenium library (pip install selenium)
- Google Chrome (latest version)
- ChromeDriver matching your Chrome version (or let Selenium Manager handle it automatically)
- A code editor or terminal
Step-by-Step
- Use the right Chrome options. Start with a minimal set of options. Avoid adding unnecessary flags that can slow things down. A basic set that works well:
Thefrom selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() options.add_argument('--headless=new') # Use the new headless mode options.add_argument('--disable-gpu') # Sometimes helps on macOS options.add_argument('--no-sandbox') # May be needed in some environments options.add_argument('--disable-dev-shm-usage') # For limited /dev/shm--headless=newflag is faster and more stable than the old headless mode. Avoid adding--window-sizeunless you need it; a smaller viewport can reduce rendering time. - Enable page load strategy. By default, Selenium waits for the full page load (including all subresources). Set it to
eagerornoneto speed up navigation:
Or useoptions.page_load_strategy = 'eager' # Waits for DOMContentLoaded'none'if you don't need to wait for any specific load state—just be careful with timing. - Disable images and CSS. If you're scraping or testing text-heavy pages, blocking images and stylesheets can dramatically reduce load time. Use Chrome's
--blink-settingsflag:
This disables image loading. For CSS, you can use a browser extension or a proxy, but that's more complex. Alternatively, useoptions.add_argument('--blink-settings=imagesEnabled=false')--disable-features=NetworkService(not recommended) or just accept CSS loading. - Reuse the browser instance. If you're running multiple tests or scraping multiple URLs, do not create a new driver for each page. Instead, reuse the same driver and navigate with
driver.get(). This saves the overhead of launching a new browser process each time. - Use implicit waits wisely. Implicit waits apply to every element lookup. Setting a high implicit wait (e.g., 10 seconds) can slow down scripts that look for many elements. Set it to a low value like 2 seconds, and use explicit waits only where needed:
For elements that appear after AJAX, usedriver.implicitly_wait(2)WebDriverWaitwith a specific condition instead of a blanket implicit wait. - Minimize element lookups. Cache WebElement objects if you need to use them multiple times. For example, if you're iterating over a list of items, store the list once and then loop through it, rather than re-querying the DOM each time.
- Use headless mode with a real user agent. Some sites serve different content to headless browsers, which can be slower or blocked. Set a normal user agent to avoid that:
This can also prevent some anti-bot measures that slow down the session.options.add_argument('user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36') - Run in parallel with threads or processes. If you have multiple independent tasks, use Python's
concurrent.futuresto run several headless browsers at once. Each thread/process gets its own driver. Be careful with resource usage—don't spawn too many at once. - Keep ChromeDriver updated. Outdated drivers can cause unnecessary waits or retries. Use Selenium Manager (built into Selenium 4.6+) to automatically download the correct driver, or manually download the latest from the Chrome for Testing site.
- Profile and measure. Use
time.time()around your code to see where the bottleneck is. If it's network latency, maybe you can't do much. If it's element lookup, optimize your selectors (use IDs and CSS classes over XPath).
Common Mistakes
- Using the old headless mode. The old
--headlessflag is slower and less stable. Always use--headless=newin recent Chrome versions. - Adding too many options. Each extra flag can add overhead. Only include what you need.
- Setting implicit wait too high. A 10-second implicit wait means every element lookup waits up to 10 seconds if the element isn't found immediately—this can add up fast.
- Creating a new driver for every URL. This is the biggest performance killer. Always reuse the driver.
- Not using
page_load_strategy. The default isnormal, which waits for all resources. Switching toeagercan cut load times significantly. - Forgetting to close the driver. Leaving drivers open consumes memory and can slow down subsequent runs. Use
driver.quit()in afinallyblock.
Tips
- Use
driver.set_page_load_timeout(10)to avoid hanging on slow pages. - If you're scraping, consider using
requests+BeautifulSoupfor static content and only use Selenium for dynamic parts. - Use
--disable-extensionsto avoid loading any extensions that might slow things down. - On macOS, you can also use
--disable-features=Translateto disable the translation service. - For very long scripts, consider using
driver.execute_script()to run JavaScript directly for things like scrolling or clicking, which can be faster than Selenium's native methods.
Troubleshooting
- Chrome doesn't start in headless mode. Make sure you're using
--headless=newand that Chrome is up to date. If you get a “Chrome failed to start” error, try adding--no-sandboxand--disable-dev-shm-usage. - Pages load slowly even with optimizations. Check if the site itself is slow. Use Chrome's DevTools (in non-headless mode) to see network requests. If it's a specific resource, you can block it with a proxy or use
--blink-settingsto disable images. - Elements not found after speed-up. If you set
page_load_strategytoeager, some elements may not be ready. Increase explicit waits for those specific elements. - Driver version mismatch. If you get a “session not created” error, delete the cached driver and let Selenium Manager download the correct one, or manually download the matching ChromeDriver.
- Memory usage is too high. Running multiple parallel drivers can exhaust memory. Reduce the number of parallel workers or add a small delay between tasks.
Frequently Asked Questions
Does headless mode actually make Selenium faster?
Yes, because it skips rendering the UI, which saves CPU and GPU cycles. However, the difference may be small on modern Macs; the bigger gains come from the other optimizations listed above.
Is it better to use --headless=new or the old --headless?
Always use --headless=new if your Chrome version supports it (Chrome 109+). It’s faster and behaves more like the regular browser, reducing compatibility issues.
Can I use Safari instead of Chrome for headless testing?
Yes, Safari has a headless mode (via safaridriver), but it’s less commonly used and has fewer options. Chrome is generally more versatile and faster for automation.
Why is my script still slow after following these steps?
Network latency and server response time are often the real bottlenecks. Try running the same script against a local test page to see if the slowness is external.
Should I use implicit or explicit waits?
Use implicit waits sparingly (low value like 1-2 seconds) and rely on explicit waits for elements that appear dynamically. This avoids unnecessary waiting.
Can I run headless Chrome without installing ChromeDriver?
Yes, Selenium 4.6+ includes Selenium Manager, which automatically downloads and manages ChromeDriver for you. Just install Selenium and it will handle the driver.