【问题标题】:selenium throws error if an instance of firefox is already open如果 Firefox 的实例已经打开,selenium 会抛出错误
【发布时间】:2021-10-05 10:24:02
【问题描述】:

我正在使用以下代码搜索谷歌并点击第一个搜索结果。

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))

但是,如果打开了一个 firefox 实例并且我运行脚本,它会给出以下消息:

Firefox 已经在运行,但没有响应。要使用 Firefox,您 必须先关闭现有的 Firefox 进程,重新启动您的设备,或者 使用不同的配置文件。

程序因以下错误而终止:

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

即使firefox的实例已经打开,我应该怎么做才能没有错误?

【问题讨论】:

    标签: python python-3.x selenium-webdriver


    【解决方案1】:

    有时,Selenium 脚本会打开 Web 驱动程序,而不是正确关闭它们。

    一个好的实践将是一个好的try/except/finally 块:

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

    此外,您应该尝试杀死作为 python 脚本的一部分在您的系统上运行的任何 firefox 进程,使用如下内容:

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

    【讨论】:

    • 有什么方法可以在新进程/窗口中打开而不杀死任何东西?
    • 我认为在多线程上下文中是可能的。您可以通过创建多个驱动程序对象并管理它们来调用多个浏览器会话。
    猜你喜欢
    • 1970-01-01
    • 2014-06-30
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    • 2021-06-24
    • 1970-01-01
    • 2020-07-19
    • 2016-07-07
    相关资源
    最近更新 更多