Selenium webdriverをChrome headless modeでPythonから使う
PhantomJSがSeleniumのサポートをやめてしまったため,
これからはヘッドレスブラウザはFireFoxかChromeになるみたいです.
macOSのGoogle Chrome Canaryだと起動場所は
/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary
になります.
Aliasに登録しても良いしコードにゴリゴリパスを書いても良いです.
起動オプションで --headless
を付ければTerminalからヘッドレスブラウザが立ち上がります.
/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary --headless https://www.google.co.jp
*Terminal等から実行する場合,スペースの前に\
を忘れないように!
# coding: UTF-8 import os from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.chrome.options import Options import time chrome_options = Options() chrome_options.add_argument("--headless") chrome_options.binary_location = '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary' wd = webdriver.Chrome(executable_path=os.path.abspath("chromedriver"), chrome_options=chrome_options) wd.set_window_size(1020,680) wd.get('http://www.google.co.jp') time.sleep(3) wd.save_screenshot("_____screenshot.png") wd.quit()