【问题标题】:How to connect to an existing firefox instance using selenium(python)如何使用 selenium(python) 连接到现有的 firefox 实例
【发布时间】:2022-06-28 23:35:41
【问题描述】:

有什么方法可以打开 Firefox 浏览器,然后使用 selenium 连接到它?我知道这可以在 chrome 上通过在命令行中启动并使用 --remote-debugging-port 参数来实现,如下所示:

import subprocess
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


subprocess.Popen('"C:/Program Files (x86)/Google/Chrome/Application/chrome.exe" --remote-debugging-port=9222', shell=True)
        
options = Options()
options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
driver = webdriver.Chrome(executable_path=PATH, options=options)

这可以在Firefox中完成吗?我一直在搜索和检查与此相关的问题一段时间,但没有运气。
我发现的唯一线索是 geckodriver 有一个 --connect-existing 参数,但我不知道如何使用它。如何将参数传递给 geckodriver 并在 selenium 中使用?

任何帮助将不胜感激。如果无法完成,请告诉我。谢谢

编辑: 好的,我已经取得了一些进展,我知道如何将 geckodriver args 传递给 selenium:

driver = webdriver.Firefox(service=Service(PATH, service_args=['--marionette-port', '9394', '--connect-existing']))

现在的问题是即使我使用这样的调试器服务器启动 Firefox:
firefox.exe -marionette -start-debugger-server <PORT>
当我运行代码时,它会引发以下错误消息:

Traceback (most recent call last):
  File "c:\Users\maxis\Desktop\Python\Freelance\Application for Opening Web Browsers\browsers\firefox.py", line 107, in <module>
    driver = webdriver.Firefox(service=Service(PATH, service_args=['--marionette-port', '9394', '--connect-existing']))
  File "C:\Users\maxis\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 180, in __init__
    RemoteWebDriver.__init__(
  File "C:\Users\maxis\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 275, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Users\maxis\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 365, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Users\maxis\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 430, in execute
    self.error_handler.check_response(response)
  File "C:\Users\maxis\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: No connection could be made because the target machine actively refused it. (os error 
10061)

或者我收到多个弹出窗口,告诉我有一个传入 Firefox 的请求。即使我点击确定,似乎也没有发生任何事情。

【问题讨论】:

    标签: python selenium selenium-webdriver firefox geckodriver


    【解决方案1】:

    我得到了同样的错误,但是当我使用默认的 Marionette 端口 2828 时它起作用了。在 Firefox 中转到 about:config 并查找 marionette.port,并确保它与 Web 驱动程序中的端口相同service_args。然后,只需使用 -marionette 选项而不是 -start-debugger-server 选项来启动 Firefox 实例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-28
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      • 2017-10-31
      • 1970-01-01
      相关资源
      最近更新 更多