pythonselenium-webdriverfirefoxgeckodriver

How to connect to an existing firefox instance using selenium (python)


Is there any way to open a Firefox browser and then connect to it using selenium? I know this is possible on chrome by launching it in the command line and using --remote-debugging-port argument like this:

import subprocess
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


subprocess.Popen('"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe" --remote-debugging-port=9222', shell=True)
        
options = Options()
options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
driver = webdriver.Chrome(executable_path=PATH, options=options)

Can this be done in firefox? I have been searching and checking questions relating to this for a while now but no luck.
The only lead I found is that geckodriver has a --connect-existing argument but I am not sure how to use it. How do you pass arguments to geckodriver and use it in selenium?

Any help would be appreciated. If it can't be done please let me know. Thank you

EDIT: Okay I have made some progress, I know how to pass geckodriver args to selenium:

driver = webdriver.Firefox(service=Service(PATH, service_args=['--marionette-port', '9394', '--connect-existing']))

The problem now is even though i start firefox with a debugger server like this:
firefox.exe -marionette -start-debugger-server <PORT>
When I run the code it either raises this error message:

Traceback (most recent call last):
  File "c:\Users\maxis\Desktop\Python\Freelance\Application for Opening Web Browsers\browsers\firefox.py", line 107, in <module>
    driver = webdriver.Firefox(service=Service(PATH, service_args=['--marionette-port', '9394', '--connect-existing']))
  File "C:\Users\maxis\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 180, in __init__
    RemoteWebDriver.__init__(
  File "C:\Users\maxis\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 275, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Users\maxis\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 365, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Users\maxis\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 430, in execute
    self.error_handler.check_response(response)
  File "C:\Users\maxis\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: No connection could be made because the target machine actively refused it. (os error 
10061)

or I get multiple popups, that tell me there is an incoming request to Firefox. Even when I click okay, nothing seems to happen.


Solution

  • CMD:

    C:\Program Files\Mozilla Firefox\
    
    firefox.exe -marionette -start-debugger-server 2828 //only use 2828
    

    Python Script:

    from selenium import webdriver
    
    driver = webdriver.Firefox(executable_path = "YOUR GECKODRIVER PATH", service_args = ['--marionette-port', '2828', '--connect-existing'] )
    
    pageSource = driver.page_source
    print(pageSource)