How to disable ghostdriver.log when using selenium

Last updated on by michaelyin

When I am using selenium with phantomjs, it will always create a log file called ghostdriver.log in the directory, which is very annoying sometime.

So I dive into the source code and find a way to fix this problem.

Here is the code to launch phantomjs, the file is located in $VIRTUALENV/lib/python2.7/site-packages/selenium/webdriver/phantomjs/

self.port = port
self.path = executable_path
self.service_args= service_args
if self.port == 0:
    self.port = utils.free_port()
if self.service_args is None:
    self.service_args = []
self.service_args.insert(0, self.path)
self.service_args.append("--webdriver=%d" % self.port)
self.process = None
if not log_path:
    log_path = "ghostdriver.log"
self._log = open(log_path, 'w')

    self.process = subprocess.Popen(self.service_args, stdin=subprocess.PIPE,
                                    close_fds=platform.system() != 'Windows',
                                    stdout=self._log, stderr=self._log)

except Exception as e:
    raise WebDriverException("Unable to start phantomjs with ghostdriver.", e)

As you can see, the ghostdriver.log is used to print the stdout and stderr of the process. We can edit the code to disable the ghostdriver.log.

Solution 1

Change the path of the log file

driver = webdriver.PhantomJS(service_log_path='/tmp/ghostdriver.log')

Solution 2

Since I found phantomjs have command option of log level, we can change the log level

driver = webdriver.PhantomJS(service_args=["--webdriver-loglevel=ERROR"])

Some people set the log level to NONE, it also seems to work but I did not find the para in phantomjs -h, but you can try it

BTW, if you want to see the verbose log, just change the ERROR to DEBUG


Java PhantomJSDriver disable all logs in console

Send Me Message

Tell me more about your project and see if I can help you.

Contact Me