Python selenium enable logging with Firefox webdriver

柔情痞子 提交于 2021-01-29 09:30:22

问题


There seems to be a problem with my implementation but I'm not sure what the problem might be. It would be really helpful if I could check the log files (as mentioned in the exception):

WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details.

but the process doesn't write anything to the file I designate as the log file.

from selenium import webdriver
from selenium.webdriver import FirefoxProfile, FirefoxOptions

log_file_handle = open('/home/plonca/selenium_draft/log_firefox_3.txt', 'w')
firefox_binary = webdriver.firefox.firefox_binary.FirefoxBinary(firefox_path='/mnt/c/Program Files/Mozilla Firefox/firefox.exe', log_file=log_file_handle) 

f_profile = FirefoxProfile('/home/plonca/selenium_draft/firefox_profiles')
options = FirefoxOptions()
options.profile = f_profile
options.log.level = 'trace'

firefox_driver = webdriver.Firefox(executable_path='/home/plonca/python_virtual_environments/selenium_jupyter_venv/bin/geckodriver.exe', firefox_binary=firefox_binary, options=options)

The full error stack:

---------------------------------------------------------------------------
WebDriverException                        Traceback (most recent call last)
<ipython-input-79-b8c3f0371d0b> in <module>
      1 firefox_driver = webdriver.Firefox(executable_path='/home/plonca/python_virtual_environments/selenium_jupyter_venv/bin/geckodriver.exe',
      2                                 firefox_binary=firefox_binary,
----> 3                                   options=options)

~/python_virtual_environments/selenium_jupyter_venv/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py in __init__(self, firefox_profile, firefox_binary, timeout, capabilities, proxy, executable_path, options, service_log_path, firefox_options, service_args, desired_capabilities, log_path, keep_alive)
    189 
    190             executor = ExtensionConnection("127.0.0.1", self.profile,
--> 191                                            self.binary, timeout)
    192             RemoteWebDriver.__init__(
    193                 self,

~/python_virtual_environments/selenium_jupyter_venv/lib/python3.6/site-packages/selenium/webdriver/firefox/extension_connection.py in __init__(self, host, firefox_profile, firefox_binary, timeout)
     50         self.profile.add_extension()
     51 
---> 52         self.binary.launch_browser(self.profile, timeout=timeout)
     53         _URL = "http://%s:%d/hub" % (HOST, PORT)
     54         RemoteConnection.__init__(

~/python_virtual_environments/selenium_jupyter_venv/lib/python3.6/site-packages/selenium/webdriver/firefox/firefox_binary.py in launch_browser(self, profile, timeout)
     71 
     72         self._start_from_profile_path(self.profile.path)
---> 73         self._wait_until_connectable(timeout=timeout)
     74 
     75     def kill(self):

~/python_virtual_environments/selenium_jupyter_venv/lib/python3.6/site-packages/selenium/webdriver/firefox/firefox_binary.py in _wait_until_connectable(self, timeout)
    102                 # Browser has exited
    103                 raise WebDriverException(
--> 104                     "The browser appears to have exited "
    105                     "before we could connect. If you specified a log_file in "
    106                     "the FirefoxBinary constructor, check it for details.")

WebDriverException: Message: The browser appears to have exited before we could connect. If you specified a log_file in the FirefoxBinary constructor, check it for details.

How to enable writing logs to /home/plonca/selenium_draft/log_firefox_3.txt? I'm using WSL: the code is run on Ubuntu 18.04 and the Firefox browser is installed on the host Windows.

  • selenium: 3.141.0
  • Firefox: 82.0.2
  • geckodriver: 0.27.0

来源:https://stackoverflow.com/questions/64662243/python-selenium-enable-logging-with-firefox-webdriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!