Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 896
Demaxl Avatar asked Jun 19 '26 23:06

Demaxl


2 Answers

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)
like image 127
eLBAS Avatar answered Jun 21 '26 14:06

eLBAS


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.

like image 45
mcvain Avatar answered Jun 21 '26 14:06

mcvain



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!