Ahmad Ismail
Ahmad Ismail

Reputation: 13872

selenium throws error if an instance of firefox is already open

I am using the following code to search google and click on first search result.

from selenium import webdriver
import urllib.parse
import time
from selenium.webdriver.firefox.options import Options

options = Options()
options.set_preference("dom.popup_maximum", 100)
options.add_argument("-profile")
options.add_argument("/home/blueray/.mozilla/firefox/5ertyoox.default-release")
options.page_load_strategy = 'eager'
# options.add_extension('fhnegjjodccfaliddboelcleikbmapik.crx')
browser = webdriver.Firefox(options=options)

with open("google-search-terms.adoc") as fin:
    for line_no, line in enumerate(fin):
        line = line.strip()
        query = urllib.parse.urlencode({'q': line + " site:amazon.com"})
        browser.execute_script(f"window.open('https://www.google.com/search?{query}');")

time.sleep(5)

for x in range(1, len(browser.window_handles)):
    browser.switch_to.window(browser.window_handles[x])
    try:

        elm = browser.find_elements_by_xpath(
            '/html/body/div[7]/div/div[9]/div[1]/div/div[2]/div[2]/div/div/div[1]/div/div/div[1]/a/h3')

        if not elm:
            elm = browser.find_elements_by_xpath(
                '/html/body/div[7]/div/div[9]/div[1]/div/div[2]/div[2]/div/div/div[1]/div/div/div/div[1]/a/h3')

        elm[0].click()

    except Exception as e:
        print("Error", str(e))

However, if one instance of firefox is open and I run the script it gives the message:

Firefox is already running, but is not responding. To use Firefox, you must first close the existing Firefox process, restart your device, or use a different profile.

And the program is terminated with the following error:

Traceback (most recent call last):
  File "google-search-amazon-less-captcha.py", line 13, in <module>
    browser = webdriver.Firefox(options=options)
  File "/home/blueray/.local/lib/python3.8/site-packages/selenium/webdriver/firefox/webdriver.py", line 170, in __init__
    RemoteWebDriver.__init__(
  File "/home/blueray/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/home/blueray/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/home/blueray/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/blueray/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Process unexpectedly closed with status 0

What should i do so that there is no error even if an instance of firefox is already open?

Upvotes: 3

Views: 1336

Answers (3)

John Hon
John Hon

Reputation: 203

options = Options()    
options.profile = r"C:\Users\XXX\AppData\Roaming\Mozilla\Firefox\Profiles\XXX.XXX"
driver = webdriver.Firefox(service = s, options=options)

This was a very annoying problem but for some reason - I've found the above code works. I got it from: https://github.com/SeleniumHQ/selenium/issues/11028

Upvotes: 0

Tyler
Tyler

Reputation: 549

I'm having the same issue but only if the open firefox instance has the same profile that I'm loading in the script. If you remove the profile from this script it should run. It should also work if your code uses a different profile that the current open window is using.

You can also use the deprecated selenium 3 way of loading a profile and this avoids the error for me.

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile


ffOptions = Options()
ffProfile = FirefoxProfile(r'C:\Users\Tyler\AppData\Roaming\Mozilla\Firefox\Profiles\0753x1pz.default')
ffOptions.profile = ffProfile

driver = webdriver.Firefox(options=ffOptions)
driver.get("http://www.google.com")

I'm still looking for a viable solution using the selenium 4 way of setting a profile.

Upvotes: 2

BlackMath
BlackMath

Reputation: 1848

Sometime, Selenium scripts are leaving web drivers open, not closing them properly.

A good pratice would be a good try/except/finally block:

driver = webdriver.Firefox()
try:
    # Your code
except Exception as e:
    # Log or print error
finally:
    driver.close()
    driver.quit()

Also, you should trying to kill any firefox processes running on your system as part of a python script, using something like this:

.....
.....
import os
os.system("taskkill /im geckodriver.exe /f")
os.system("taskkill /im firefox.exe /f")

Upvotes: 0

Related Questions