Description
What happened?
I'm trying to launch Chrome with the --headless flag and --remote-debugging-port=9222 for automation purposes. However, when I run Chrome in headless mode, the WebDriver throws a SessionNotCreatedException, and it can't connect to Chrome on port 9222. When I remove the --headless flag, everything works fine.
Has anyone encountered this issue before? Is there a known limitation with remote debugging in headless mode, or is there a workaround for this? I'm using Chrome version [insert version] and ChromeDriver version [insert version]. Any help would be appreciated!
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.select import Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException
class WebDriverInstance:
def __init__(self, port, headless=False, download_directory=None, timeout=10):
self.port = port
self.headless = headless
self.download_directory = download_directory
self.timeout = timeout
self.driver : webdriver.Chrome = self._create_webdriver()
def _create_webdriver(self) -> webdriver.Chrome:
chrome_options = webdriver.ChromeOptions()
chrome_options.debugger_address = f"localhost:{self.port}"
if self.headless:
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--window-size=1920x1080")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--disable-dev-shm-usage")
try:
driver = webdriver.Chrome(options=chrome_options)
return driver
except Exception as e:
raise Exception(f"Erro ao criar WebDriver na porta {self.port}: {e}")
Just to be clear... If I open a chrome instance manually with google-chrome --remote-debugging-port=9222
and run webdriver.Chrome(options=chrome_options)
without --headless
, it connects.
If I let webdriver.Chrome(options=chrome_options)
instantiate chrome with --headless
, it also works.
What doesn't work are both things.
What browsers and operating systems are you seeing the problem on?
- using selenium/standalone-chrome:127.0 docker image as enviroment
- ChromeDriver 127.0.6533.119 (bdef6783a05f0b3f885591e7d2c7b2aec1a89dea-refs/branch-heads/6533@{chore(deps): update dependency rubocop to v1.67.0 #1999})
- Google Chrome 127.0.6533.119
Activity
github-actions commentedon Sep 19, 2024
@Batchuka, thank you for creating this issue. We will troubleshoot it as soon as we can.
Info for maintainers
Triage this issue by using labels.
If information is missing, add a helpful comment and then
I-issue-template
label.If the issue is a question, add the
I-question
label.If the issue is valid but there is no time to troubleshoot it, consider adding the
help wanted
label.After troubleshooting the issue, please add the
R-awaiting answer
label.Thank you!