I’m working on automating browser testing with Selenium and I have a working setup for Edge that launches the browser in remote debugging mode and then connects the WebDriver to it. Here is the code I use for Edge:
import subprocess
from selenium import webdriver
from selenium.webdriver.edge.service import Service as EdService
from selenium.webdriver.edge.options import Options as EdOptions
from webdriver_manager.microsoft import EdgeChromiumDriverManager
class BrowserManager:
def browserEdge(self, port=8989):
'''This function will return an Edge browser instance with remote debugging enabled.'''
driverPath = EdgeChromiumDriverManager().install()
subprocess.Popen(
["start", "msedge", f"--remote-debugging-port={port}"], shell=True)
options = EdOptions()
options.add_argument("--start-maximized")
options.add_experimental_option("debuggerAddress", f"localhost:{port}")
service = EdService(executable_path=driverPath)
driver = webdriver.Edge(service=service, options=options)
driver.implicitly_wait(3)
return driver
Now, I’m trying to achieve the same setup for Firefox. I attempted the following code, but it ended up opening two instances of Firefox, and the WebDriver does not connect to the instance started with subprocess
:
import subprocess
from selenium import webdriver
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.firefox.options import Options as FirefoxOptions
from webdriver_manager.firefox import GeckoDriverManager
class BrowserManager:
def browserFirefox(self, port=9222):
'''This function will return a Firefox browser instance with remote debugging enabled.'''
driverPath = GeckoDriverManager().install()
subprocess.Popen(
["start", "firefox", f"--start-debugger-server={port}"], shell=True)
options = FirefoxOptions()
options.add_argument("--start-maximized")
options.set_preference("devtools.debugger.remote-enabled", True)
options.set_preference("devtools.debugger.remote-port", port)
options.set_preference("devtools.debugger.prompt-connection", False)
service = FirefoxService(executable_path=driverPath)
driver = webdriver.Firefox(service=service, options=options)
driver.implicitly_wait(3)
return driver
if __name__ == "__main__":
bm = BrowserManager()
driver = bm.browserFirefox()
driver.get("https://www.google.com")
driver.quit()
I need the WebDriver to connect to the already running instance of Firefox that was started with the subprocess command.
Can someone help me fix the code so that it works as intended, similar to how my Edge setup works?