---
title: "How to Run Selenium in Headless Mode with Python in 2026"
description: "Selenium is used to run browser tests and optimize web scraping. Wondering how to run Selenium in headless mode? Read on to find out!"
url: "https://iproyal.com/blog/headless-browser-python/"
---

- [EN - English](https://iproyal.com/)
   
  [中文 - Chinese](https://iproyal.cn/)

[Book a Demo](https://iproyal.com/request-a-demo/) [Create Account](https://dashboard.iproyal.com/register/) [Create Account](https://dashboard.iproyal.com/register/) [Login](https://dashboard.iproyal.com/login/) [Login](https://dashboard.iproyal.com/login/)

[Book a Demo](https://iproyal.com/request-a-demo/)

[Back to blog](https://iproyal.com/blog/)

# How to Run Selenium Headless in Python: Step-by-Step Guide

[Vilius Dumcius](https://iproyal.com/blog/authors/vilius-dumcius/)

[Last updated - January 23, 2024 ‐ 7 min read](https://iproyal.com/blog/authors/vilius-dumcius/)

[Selenium](https://iproyal.com/blog/category/selenium/)

Selenium is an umbrella project that provides tools and libraries for browser automation across many programming languages, including Python. In simple terms, **Selenium lets you run browsers without a graphical interface and test web applications.&#x20;**&#x53;elenium’s headless browser is also widely used for web scraping purposes as **they are much faster and less resource intensive than headful browsers.&#x20;**&#x52;unning Selenium headless in Python also provides more room for optimization and better programmatic control.

## Preparation for Running Selenium

Before you can run a headless browser with Selenium, **you’ll need to get Python and an IDE.&#x20;**&#x59;ou can [download Python from their official website ](https://www.python.org/downloads/). There are many IDEs available with various different features, but a good starting point for Python browser emulation is [PyCharm Community Edition ](https://www.jetbrains.com/pycharm/download/?section=windows). **It’s a free IDE that works perfectly fine for all Python programming.**

Once both are installed, **open PyCharm and start a new project.**

Then, **you’ll need to install the Selenium library.&#x20;**&#x59;ou can do that by opening up the Terminal in PyCharm and typing in:

```javascript
pip install selenium
```

**PyCharm will then automatically download, extract, and install the Selenium library.&#x20;**&#x49;t'll only be available for the current project. While you can install them permanently, doing so is not necessary for smaller projects.

Finally, you may have to find a headless webdriver. **Headless Selenium runs by using existing browser webdrivers.&#x20;**&#x43;urrent versions should come with Webdriver versions for all popular browsers.

### Alternative: Install Selenium Using WebDrive Manager

Instead of manually downloading the Chrome WebDriver, **you can use the WebDriver Manager package.&#x20;**&#x49;t saves time and avoids version mismatch issues. Install it by running this:

```javascript
pip install webdriver-manager
```

Now, **modify your code&#x20;**&#x6C;ike this:

```python
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
```

And use it with:

```python
browser = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
```

**You can then use *browser.get()* and any other methods as per usual.&#x20;**&#x54;he installation only runs once (if the driver does not exist), otherwise it checks if the appropriate version is present, so there’s not a lot of additional overhead.

Webdriver-manager is great because it handles everything behind the scenes. **It’s great for smoother headless browser testing and quick setups.&#x20;**&#x59;ou don’t have to direct your code to specific paths or put the driver in your project folder.

## Running Selenium in Headful Mode

We'll first **test Selenium in headful mode&#x20;**&#x74;o ensure everything is running correctly.

Start by **importing webdriver from Selenium:**

```python
from selenium import webdriver
```

Then, we'll **define a function that takes an argument for the URL:**

```python
def open_browser(URL: str):
    browser = webdriver.Chrome()
    browser.get(URL)
    browser.quit()

open_browser('https://iproyal\.com/')
```

Our first line defines the function and its argument (URL). Although adding “: str” is not necessary, **it limits the acceptable inputs to only strings, which reduces the likelihood of human error.&#x20;**&#x4D;ake sure to remove it, if you plan on using other data formats.

**“Browser” creates an object with the value of “webdriver.Chrome()”,&#x20;**&#x77;hich essentially creates a webdriver object we'll later use to perform certain actions.

Then, **the “browser.get(URL)” uses the browser object's method “get” to go to a URL.&#x20;**&#x41;fter the page loads fully, the browser should exit automatically due to “browser.quit()”.

Finally, **we call the function “open\_browser”&#x20;**&#x77;ith our desired URL in between parentheses and quotation marks.

Let's **execute the code by clicking the green arrow at the top right.&#x20;**&#x49;f everything works correctly, a browser should open up automatically and automatically go to [our homepage ](https://iproyal.com/).

**If you get a missing webdriver error when executing code, you'll have to download the webdriver manually.&#x20;**&#x53;ince we'll be using the Chrome webdriver, you can download it from the [testing repository ](https://googlechromelabs.github.io/chrome-for-testing/#stable). Make sure to download “chromedriver”, appropriate for your OS.

## Switching to Selenium Headless Mode

In our previous example, a browser opened up and visited our homepage with the full GUI. **For testing and web scraping purposes, most opt to run Selenium headless.**

We'll have to **add another import that will allow us to adjust the Selenium Chrome options&#x20;**&#x66;or headless Chrome.

```python
from selenium import webdriver
from selenium.webdriver import ChromeOptions
```

Now **three new lines of code&#x20;**&#x77;ill have to be added to our function:

```python
def open_browser(URL: str):
    options = ChromeOptions()
    options.add_argument("--headless=new")
    browser = webdriver.Chrome(options=options)
    browser.get(URL)
    browser.quit()

open_browser('https://iproyal\.com/')
```

**“Options” creates an object to store our Chrome headless mode options.&#x20;**&#x57;e'll then use the “.add\_argument” method to add a string that tells the browser to run headless Chrome.

Finally, we need to **adjust the “browser” object to include our Chrome headless Selenium commands.**

We can now **execute our code again&#x20;**&#x62;y clicking the same green arrow.

Note that “nothing” will happen as we have removed the UI by running a headless browser. **Python gives you a few more options that make Selenium's headless mode even better.**

## Running Through a List of URLs

In almost all cases, **you'll be using Selenium to run through a larger list of URLs&#x20;**&#x69;nstead of a single one. We can do that by changing our function:

```python
def open_browser(URL):
    options = ChromeOptions()
    options.add_argument("--headless=new")
    browser = webdriver.Chrome(options=options)
    for i in URL:
        browser.get(i)
    browser.quit()
```

Our function will now accept sequence objects (lists, tuples, dictionaries, etc) as we have used the “for” loop. When calling the function, **we'll have to input one of those objects and then the browser will visit each URL in sequence.**

We also need to **add a dictionary object:**

```python
list_of_urls = ['https://iproyal.com/', 'https://iproyal.com/pricing/residential-proxies/']
open_browser(list_of_urls)
```

Each object in the dictionary is separated by a comma and you can add as many as you like. However, **the current function will not accept a single string that's not in a sequence object.**

Before you execute the code, it's recommended to comment out (by using # to the left of a line of code) the “.add\_argument” method. Otherwise, **Python's Selenium headless mode will be active and you won't be able to see if everything is happening as it should.**

Here's the full code block:

```python
from selenium import webdriver
from selenium.webdriver import ChromeOptions

def open_browser(URL):
    options = ChromeOptions()
    options.add_argument("--headless=new")
    browser = webdriver.Chrome(options=options)
    for i in URL:
        browser.get(i)
    browser.quit()

list_of_urls = ['https://iproyal.com/', 'https://iproyal.com/pricing/residential-proxies/']
open_browser(list_of_urls)
```

## Headless Web Scraping Tutorial With Selenium

Going one step further, **you can perform headless web scraping with Python using Selenium.&#x20;**&#x46;or example’s sake, we’ll scrape the title of a webpage, simulate a wait for AJAX requests, and save the output.

Start with:

```javascript
pip install selenium webdriver-manager
```

```python
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
import time

def scrape_title(url: str) -> None:
    """
    Scrapes the title of a given webpage using Selenium with a headless Chrome browser,
    saves it to a file named 'page_title.txt', and closes the browser.

    Args:
        url (str): The full URL of the webpage to scrape.

    Returns:
        None: The function does not return a value. The extracted page title is written
              to 'page_title.txt'.
    """

    options = ChromeOptions()
    options.add_argument("--headless=new")
    browser = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
    
    browser.get(url)
    
    time.sleep(2)  
    
    title = browser.title
    
    with open("page_title.txt", "w") as file:
        file.write(title)
    
    browser.quit()

scrape_title('https://iproyal.com/')
```

Here’s what’s happening:

- We’re using find\_element to grab the page title.
- time.sleep() gives time for AJAX requests to finish.
- The title is saved to a file, which is useful for keeping static data.

It’s perfect for beginners doing Python web scraping without a browser. Even though it runs without a UI, you can still get the content you need. **Handling a headless browser with Python this way is clean and fast.**

## Selenium Headless Chrome Options in Python

Selenium’s Chrome options allow customization for **headless browser testing, scraping, and automation.**

Here’s a breakdown of common options:

```python
options = ChromeOptions()

options.add_argument("--headless=new")             
options.add_argument("--disable-gpu")              
options.add_argument("--no-sandbox")               
options.add_argument("--window-size=1920,1080")    
options.add_argument("user-agent=MyScraperBot") 
```

Each flag does its own thing:

- ***--headless=new&#x20;***&#x74;urns on headless Chrome browser.
- ***--disable-gpu&#x20;***&#x64;isables GPU hardware acceleration, rarely needed except for specific rendering issues.
- ***--no-sandbox&#x20;***&#x6C;ets headless script execution in Selenium work in containers.
- ***--window-size&#x20;***&#x73;ets window size for rendering and screenshots.
- ***User-agent&#x20;***&#x73;ets custom user-agent string for HTTP requests.

Here’s how to use all of them together:

```python
options = ChromeOptions()
options.add_argument("--headless=new")
options.add_argument("--disable-gpu")
options.add_argument("--no-sandbox")
options.add_argument("--window-size=1920,1080")
options.add_argument("user-agent=MyScraperBot")
```

**You now have full control over your Selenium headless browser.&#x20;**&#x54;hese settings make it easier to execute (or not) JavaScript, scrape static data, and do script execution without a full Chrome browser interface.

**It’s perfect for Selenium headless browser automation.&#x20;**&#x54;here’s even more settings you can find in the [official documentation ](https://www.selenium.dev/documentation/webdriver/drivers/options/).

## Conclusion

You only need a few lines of code to run Selenium webdriver headless. Python, however, gives you many more options and, when automating, you'll be doing more than simply opening up a browser in headless mode. It gives you reliable headless testing in Selenium with Python, from importing Selenium webdrivers to configuring Chrome options. You’ve learned the ins and outs of headless browser testing and fast script execution. With these tools, working with headless Chrome becomes simple, fast, and useful when you’re scraping data, doing headless browser testing, or automating tasks.

[Create Account](https://dashboard.iproyal.com/)

[Author](https://iproyal.com/blog/authors/vilius-dumcius/)

[Vilius Dumcius](https://iproyal.com/blog/authors/vilius-dumcius/)

[Head of Engineering](https://iproyal.com/blog/authors/vilius-dumcius/)

With six years of programming experience, Vilius specializes in full-stack web development with PHP (Laravel), MySQL, Docker, Vue.js, and Typescript. Managing a skilled team at IPRoyal for years, he excels in overseeing diverse web projects and custom solutions. Vilius plays a critical role in managing proxy-related tasks for the company, serving as the lead programmer involved in every aspect of the business. Outside of his professional duties, Vilius channels his passion for personal and professional growth, balancing his tech expertise with a commitment to continuous improvement.

[Learn More About Vilius Dumcius ](https://iproyal.com/blog/authors/vilius-dumcius/)[Meet all Writers](https://iproyal.com/blog/authors/)

Share on

### In This Article

- [Preparation for Running Selenium](https://iproyal.com/blog/headless-browser-python/#Preparation-for-Running-Selenium)
- [Running Selenium in Headful Mode](https://iproyal.com/blog/headless-browser-python/#Running-Selenium-in-Headful-Mode)
- [Switching to Selenium Headless Mode](https://iproyal.com/blog/headless-browser-python/#Switching-to-Selenium-Headless-Mode)
- [Running Through a List of URLs](https://iproyal.com/blog/headless-browser-python/#Running-Through-a-List-of-URLs)
- [Headless Web Scraping Tutorial With Selenium](https://iproyal.com/blog/headless-browser-python/#Headless-Web-Scraping-Tutorial-With-Selenium)
- [Selenium Headless Chrome Options in Python](https://iproyal.com/blog/headless-browser-python/#Selenium-Headless-Chrome-Options-in-Python)
- [Conclusion](https://iproyal.com/blog/headless-browser-python/#Conclusion)

Ready to get started?

[Register now](https://dashboard.iproyal.com/register/)

Data News in Your Inbox

No spam whatsoever, just pure data gathering news, trending topics and useful links. Unsubscribe anytime.

No spam. Unsubscribe anytime.

## Related articles

[View all](https://iproyal.com/blog/category/selenium/)

[Selenium](https://iproyal.com/blog/headless-firefox-python-selenium/)

## [How to Run Headless Firefox with Python Selenium](https://iproyal.com/blog/headless-firefox-python-selenium/)

[Find out how to run Firefox in headless mode with Selenium and when that’s useful to do when scraping or automating browsers.](https://iproyal.com/blog/headless-firefox-python-selenium/)

[August 19, 2024](https://iproyal.com/blog/headless-firefox-python-selenium/)

[5 min read](https://iproyal.com/blog/headless-firefox-python-selenium/)

[Selenium](https://iproyal.com/blog/selenium-stealth-python-tutorial/)

## [Mastering Selenium Stealth for Effective Web Scraping Techniques](https://iproyal.com/blog/selenium-stealth-python-tutorial/)

[Tired of getting blocked while scraping? Discover how to use Selenium Stealth to outsmart bot detection and browse like a real user—undetected and uninterrupted.](https://iproyal.com/blog/selenium-stealth-python-tutorial/)

[June 17, 2025](https://iproyal.com/blog/selenium-stealth-python-tutorial/)

[13 min read](https://iproyal.com/blog/selenium-stealth-python-tutorial/)

[Selenium](https://iproyal.com/blog/selenium-find-element/)

## [How to Find Elements in Selenium: A Beginner’s Guide with Examples](https://iproyal.com/blog/selenium-find-element/)

[Learn how to find elements in Selenium using Python. Discover locator types, code examples, and tips for locating web elements on any web page.](https://iproyal.com/blog/selenium-find-element/)

[December 16, 2025](https://iproyal.com/blog/selenium-find-element/)

[5 min read](https://iproyal.com/blog/selenium-find-element/)
