Home > Enterprise >  Check if there is any selenium already open with firefox in python
Check if there is any selenium already open with firefox in python

Time:10-27

I've been trying for some time to get this function to work, but I've tried several ways and I still can't. can anybody help me?

My goal is to prevent multiple browsers from being opened, that is, to check if there is already an instance of selenium firefox already open and return it so that the already open window can be used.

I tried with webdriver.remote, but no success.

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


def getDriver():
    if isAlreadyRunning() == False:
        firefox_service = Service(GeckoDriverManager().install())
        driver = webdriver.Firefox(service=firefox_service)
        return driver


def isAlreadyRunning():
    try:
        # Checks if a previous instance already exists to avoid opening a new browser
        return True
    except:
        print('Driver is not running')
        return False


driver = getDriver()
driver.get('https://stackoverflow.com')

The last attempts were activating the option marionette.debugging.clicktostart in about:config, opening cmd and starting firefox with the options:

cd 'C:\Program Files\Mozilla Firefox\'
.\firefox.exe -marionette -start-debugger-server 2828

and the code snippet below:

firefox_service = Service(GeckoDriverManager().install(), service_args=['--marionette-port', '2828', '--connect-existing'])
driver = webdriver.Firefox(service=firefox_service)

But after several seconds, selenium.common.exceptions.TimeoutException error is thrown

I'm using:

  • Python 3.10.4
  • Selenium 4.5.0

CodePudding user response:

The below snippet isn't exactly your solution, but it would show a prompt if more than one instance of firefox is commanded by the program to run. The prompt would look like this although this won't focus on the opened window, the code would start running on that already open instance of Firefox, avoiding the clutter of multiple instances.

options = Options()
options.add_argument("-profile")
options.add_argument(os.path.expanduser("~") "/AppData/Local/Mozilla/Firefox/Profiles")
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
browser = webdriver.Firefox(capabilities=firefox_capabilities, options=options , executable_path= 'Whatsapp-Automation\geckodriver\geckodriver.exe')

This code snippet would create a unique profile of Firefox, so all your logins if done here would also be saved here.

Note: These are the required imports

from selenium import webdriver
import os
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

CodePudding user response:

use psutil to check if any geckodriver is opened

import psutil
for proc in psutil.process_iter():
    if "geckodriver" in proc.name():
        print("- found..")

and use that to check firefox

for proc in psutil.process_iter():
if "firefox" in proc.name():
    print(proc.cmdline())

check cmdline if contain current args

  • Related