[Selenium] Change log output destination when executing phantomjs in python3

The log when executing phantomJS in python was a little annoying, so here's how to change the output destination.

How to run in PhantomJS

from selenium import webdriver

#Specifying the driver
driver = webdriver.PhantomJS()
driver.set_window_size(1024, 768)
driver.get('https://google.com/')

#capture
driver.save_screenshot('google.png')

driver.quit()

Default output

It is saved as ghostdriver.log under the current directory.

Change output destination

If you specify a path for service_log_path, it will be output to that path.

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

Log level settings

Specify phantomJS argument --webdriver-loglevel in service_args

log_name = '/tmp/phantomjs.log'
driver = webdriver.PhantomJS(service_log_path=log_name,service_args=["--webdriver-loglevel=ERROR"])

You can check the arguments that can be passed to phantomJS with phantomjs -h

Standard output

import os
import sys

driver = webdriver.PhantomJS(service_log_path=os.ttyname(sys.stdout.fileno()))

Do not output logs

import os

driver = webdriver.PhantomJS(service_log_path=os.path.devnull)

Recommended Posts

[Selenium] Change log output destination when executing phantomjs in python3
Change the standard output destination to a file in Python
Log in to Yahoo Business with Selenium Python
Freeze with send_keys of file selection when running Selenium WebDriver in Python [PhantomJS]
Japanese output in Python
When "Message: session not created" occurs in Python + Selenium
Output log in JSON format with Python standard logging
Use communicate () when receiving output in a Python subprocess
Japanese output when dealing with python in visual studio
Scraping with selenium in Python
Attention when os.mkdir in Python
Read Fortran output in python
[Python, Selenium, PhantomJS] A story when scraping a website with lazy load
When you run diff in python and want both returncode and output
Error log output method when using Bottle framework on GAE / Python
Output 2017 Premium Friday list in Python
Precautions when using pit in Python
Scraping with Selenium in Python (Basic)
Behavior when listing in Python heapq
[Python] Variable (definition, output, value change)
Make standard output non-blocking in Python
Unit test log output with python
Write selenium test code in python
Export and output files in Python
Precautions when using phantomjs from python
When using regular expressions in Python
When writing a program in Python
When specifying multiple keys in python sort
Create ScriptableObject in Python when building ADX2
Output tree structure of files in Python
Output Python log to console with GAE
Log in to Slack using requests in Python
When looking at memory usage in Python 3
Color scheme change when executing LS command
Reboot the router using Python, Selenium, PhantomJS
Change line breaks in iPython autoformatted output
Use selenium phantomjs webdriver with python unittest
Output timing is incorrect when standard (error) output is converted to a file in Python
[Python selenium] After scraping Google search results, output title and URL in csv