To connect to an already started Firefox instance using Python and Selenium, you need to use the marionette protocol, which is Firefox's remote control protocol. Here’s how you can do it:
First, you need to start Firefox manually with the --marionette flag and specify a port for remote debugging. Open a terminal and run:
bashfirefox --marionette --start-debugger-server 2828
--marionette enables the remote control protocol.--start-debugger-server 2828 starts the debugger server on port 2828 (you can use any available port).Now, in your Python script, use the following code to connect to the already running Firefox instance:
pythonfrom selenium import webdriver from selenium.webdriver.firefox.options import Options # Configure the connection to the existing Firefox instance options = Options() options.debugger_address = "localhost:2828" # Match the port you used # Connect to the running Firefox instance driver = webdriver.Firefox(options=options) # Now you can control the existing Firefox window driver.get("https://www.example.com") print("Connected to the existing Firefox instance!")
debugger_address matches the one you used when starting Firefox.