有没有办法打开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。即使我点击确定,似乎也没有任何反应。