How would I use selenium's headless mode in python?

111 Views Asked by At

I am using the same code that's in the recent patch notes and it's still not working.

options = ChromeOptions()
options.add_argument("--headless=new")
driver = webdriver.Chrome(options=options)

My code works without headless mode, otherwise, I get this error:

raise TimeoutException(message, screen, stacktrace)

I also tried this and it didn't work:

service = Service(executable_path=r"C:\Program Files (x86)\WebDrivers\chromedriver.exe")
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(service=service, options=options)

selenium 4.18.0 Python 3.10.7

2

There are 2 best solutions below

0
Jakob Bagterp On

The cumbersome settings of Selenium and its web drivers is one of the reasons why I created Browserist as an extension to Selenium. Headless mode simply works out of the box with Browserist.

In full disclosure, I'm the author of this package. Browserist is lightweight, less verbose extension of the Selenium web driver that makes browser automation even easier. Simply install the package with pip install browserist and you're ready to go.

Maybe something like this could solve your problem?

from browserist import Browser, BrowserSettings

settings = BrowserSettings(headless = True)

with Browser(settings) as browser:
    browser.open.url("https://example.com")

Browserist has a mature set of methods to interact with web pages, but if you prefer working with the Selenium web driver, you can also return to the standard driver and continue your script with this:

    driver = browser.driver

Note that simple settings of headless mode of Browserist work across browser types, so you can even do something like this if you already have Chrome, Edge, Firefox installed:

from browserist import Browser, BrowserSettings, BrowserType

chrome = BrowserSettings(type = BrowserType.CHROME, headless = True)
edge = BrowserSettings(type = BrowserType.EDGE, headless = True)
firefox = BrowserSettings(type = BrowserType.FIREFOX, headless = True)

for settings in [chrome, edge, firefox]:
    with Browser(settings) as browser:
        browser.open.url("https://example.com")
0
Michael Mintz On

With SeleniumBase, activating Chrome's new headless mode ("--headless=new") is just:

from seleniumbase import Driver

driver = Driver(headless2=True)

A regular script could look like this:

from seleniumbase import Driver

driver = Driver()
driver.open("seleniumbase.io/demo_page")
driver.highlight("h2")
driver.type("#myTextInput", "Automation")
driver.click("#checkBox1")
driver.highlight("img", loops=6)
driver.quit()

Or if you prefer using a context manager so that the browser always closes at the end:

from seleniumbase import DriverContext

with DriverContext() as driver:
    driver.open("seleniumbase.github.io/demo_page")
    driver.highlight("h2")
    driver.type("#myTextInput", "Automation")
    driver.click("#checkBox1")
    driver.highlight("img", loops=6)

If needed, there are a lot more SeleniumBase syntax formats to choose from.