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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With