Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[🐛 Bug]: Remote Debugging with --headless flag on Chrome #1952

Open
Batchuka opened this issue Sep 19, 2024 · 1 comment
Open

[🐛 Bug]: Remote Debugging with --headless flag on Chrome #1952

Batchuka opened this issue Sep 19, 2024 · 1 comment
Labels
bug Something isn't working needs-triaging

Comments

@Batchuka
Copy link

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?

@Batchuka Batchuka added bug Something isn't working needs-triaging labels Sep 19, 2024
Copy link
Contributor

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs-triaging
Projects
None yet
Development

No branches or pull requests

1 participant