我使用的是 Firefox 版本 53。Geckodriver 版本 0.16,selenium 版本 3.4。我想获取 webdriver 的控制台日志
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
d = DesiredCapabilities.FIREFOX
d['loggingPrefs'] = {'browser': 'ALL'}
driver = webdriver.Firefox(capabilities=d)
driver.set_window_size(1280,1024)
driver.get('http://google.com')
try:
for entry in driver.get_log('browser'):
print entry
finally:
driver.quit()
控制台输出
C:\Python27\python.exe C:/Users/GS-0758/PycharmProjects/untitled1/test.py
Starting selenium session
Traceback (most recent call last):
File "C:/Users/GS-0758/PycharmProjects/untitled1/test.py", line 5, in <module>
driver = webdriver.Firefox(capabilities=d)
File "C:\Python27\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 153, in __init__
keep_alive=True)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 99, in __init__
self.start_session(desired_capabilities, browser_profile)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 189, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 253, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: loggingPrefs was not a the name of a known capability or a valid extension capability
这是已知问题,您可以在这里查看
geckodriver 是 W3C WebDriver 的实现,它不 目前指定一个日志接口,所以这是预期的行为。
可能的解决方法是启用 Firefox 实例的远程调试并使用调试器获取日志:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
firefox_options = Options()
profile = FirefoxProfile()
profile.set_preference("devtools.chrome.enabled", True)
profile.set_preference("devtools.debugger.remote-enabled", True)
profile.set_preference("devtools.debugger.prompt-connection", False)
firefox_options.arguments.append('-start-debugger-server')
firefox_options.profile = profile
firefox_options.arguments.append('9222')
driver = webdriver.Firefox(options=firefox_options)
配置完 Firefox 实例后,您可以使用远程调试协议与其进行通信。您可以使用 telnet 或 netcat 测试配置 - 只需在建立连接后发送
30:{"type":"getRoot","to":"root"}
。还有库 geckordp,它充当远程调试协议的包装器。
注意:此解决方法适用于 Python 3.12 和 Selenium 4.24。