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

3

6

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.

Itinerary answered 21/5, 2022 at 17:28 Comment(0)
T
5

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)
Transom answered 1/9, 2022 at 14:33 Comment(2)
Really only port 2828 will work? This seems wrong, since you can set that port, and in Chrome you can run on any port you want, which allows multiple processes to automate differeent browsers on differen ports at the same time, which is super usefulPester
In new Selenium version the service_args must be provided by Service constructor.Wheaten
S
2

First Step:

Open CMD and execute: firefox.exe --marionette

This command will open a firefox instance that has its marionette-port=2828 (by default)

(writes about:config in url bar of the firefox instance, press enter and then search: marionette.port)


Then:

from selenium import webdriver
from selenium.webdriver.firefox.service import Service

firefox_services = Service(executable_path='firefoxdriver', port=3000, service_args=['--marionette-port', '2828', '--connect-existing'])
driver = webdriver.Firefox(service=firefox_services)
driver.get('https://youtube.com')
driver.execute_script('alert(\'your favorite music is here\')')

executable_path='firefoxdriver'
My geckodriver.exe is inside firefoxdriver folder

Screenshot my VSCode

port=3000
I want to send my 'geckodriver orders' throught port 3000

service_args=['--marionette-port', '2828', '--connect-existing']
I want to control an open firefox instance that has its marionette-port=2828


Suggestion:
Delete all firefox profile folders that were created in %temp% before you knew how to connect to an open firefox instance

Delete these folders in %temp%

Stoneware answered 18/11, 2022 at 1:49 Comment(1)
No matter how I start firefox, I cannot set the marionette port. It always comes out as some random number.Pester
H
1

I got the same error but it worked when I used the default Marionette port of 2828. Go to about:config in your Firefox and look up marionette.port, and make sure it is the same as the port in your web driver's service_args. Then, start a Firefox instance simply with the -marionette option but without the -start-debugger-server option.

Hainan answered 28/6, 2022 at 15:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.