https://teratail.com/questions/223722
この続きで質問させていただきます。
(なるべく、このページだけでわかるように書いたつもりです。)
解決したいこと
chromedriverをAWS内で使えるようになる。
問題のエラーコード
chromedriver.pyのファイル
python3
1from time import sleep 2from selenium import webdriver 3 4browser = webdriver.Chrome() 5 6browser.get("https://www.google.com") 7 8browser.save_screenshot("screen.png") 9 10sleep(5) 11 12browser.close()
これを実行すると、
AWS
1ec2-user:~/environment $ python3 chromedriver.py 2Traceback (most recent call last): 3 File "chromedriver.py", line 4, in <module> 4 browser = webdriver.Chrome() 5 File "/home/ec2-user/.local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__ 6 desired_capabilities=desired_capabilities) 7 File "/home/ec2-user/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__ 8 self.start_session(capabilities, browser_profile) 9 File "/home/ec2-user/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session 10 response = self.execute(Command.NEW_SESSION, parameters) 11 File "/home/ec2-user/.local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute 12 self.error_handler.check_response(response) 13 File "/home/ec2-user/.local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response 14 raise exception_class(message, screen, stacktrace) 15selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally 16 (unknown error: DevToolsActivePort file doesn't exist) 17 (The process started from chrome location /opt/google/chrome/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
各ディレクトリの配置等の環境
エラーが起こっているディレクトリの中身
AWS
1ec2-user:/opt/google/chrome $ ls 2chrome lib product_logo_16.png swiftshader 3chrome_100_percent.pak locales product_logo_24.png v8_context_snapshot.bin 4chrome_200_percent.pak MEIPreload product_logo_256.png WidevineCdm 5chrome-sandbox nacl_helper product_logo_32.png xdg-mime 6default-app-block nacl_helper_bootstrap product_logo_32.xpm xdg-settings 7default_apps nacl_irt_x86_64.nexe product_logo_48.png 8google-chrome natives_blob.bin product_logo_64.png 9icudtl.dat product_logo_128.png resources.pak
/opt/google/chrome下にあるgoogle-chromeについて
vim
1#!/bin/bash 2# 3# Copyright (c) 2011 The Chromium Authors. All rights reserved. 4# Use of this source code is governed by a BSD-style license that can be 5# found in the LICENSE file. 6 7# Let the wrapped binary know that it has been run through the wrapper. 8export CHROME_WRAPPER="`readlink -f "$0"`" 9 10HERE="`dirname "$CHROME_WRAPPER"`" 11 12# We include some xdg utilities next to the binary, and we want to prefer them 13# over the system versions when we know the system versions are very old. We 14# detect whether the system xdg utilities are sufficiently new to be likely to 15# work for us by looking for xdg-settings. If we find it, we leave $PATH alone, 16# so that the system xdg utilities (including any distro patches) will be used. 17if ! which xdg-settings &> /dev/null; then 18 # Old xdg utilities. Prepend $HERE to $PATH to use ours instead. 19 export PATH="$HERE:$PATH" 20else 21 # Use system xdg utilities. But first create mimeapps.list if it doesn't 22 # exist; some systems have bugs in xdg-mime that make it fail without it. 23 xdg_app_dir="${XDG_DATA_HOME:-$HOME/.local/share/applications}" 24 mkdir -p "$xdg_app_dir" 25 [ -f "$xdg_app_dir/mimeapps.list" ] || touch "$xdg_app_dir/mimeapps.list" 26fi 27 28# Always use our versions of ffmpeg libs. 29# This also makes RPMs find the compatibly-named library symlinks. 30if [[ -n "$LD_LIBRARY_PATH" ]]; then 31 LD_LIBRARY_PATH="$HERE:$HERE/lib:$LD_LIBRARY_PATH" 32else 33 LD_LIBRARY_PATH="$HERE:$HERE/lib" 34fi 35export LD_LIBRARY_PATH 36 37export CHROME_VERSION_EXTRA="stable" 38 39# We don't want bug-buddy intercepting our crashes. http://crbug.com/24120 40export GNOME_DISABLE_CRASH_DIALOG=SET_BY_GOOGLE_CHROME 41 42# Sanitize std{in,out,err} because they'll be shared with untrusted child 43# processes (http://crbug.com/376567). 44exec < /dev/null 45exec > >(exec cat) 46exec 2> >(exec cat >&2) 47 48# Note: exec -a below is a bashism. 49exec -a "$0" "$HERE/chrome" "$@"
seleniumの確認
AWS
1ec2-user:~/environment/ch2 $ pip install selenium 2Requirement already satisfied: selenium in /home/ec2-user/.local/lib/python3.6/site-packages (3.141.0) 3Requirement already satisfied: urllib3 in /home/ec2-user/.local/lib/python3.6/site-packages (from selenium) (1.25.7)
前提として
/usr/local/binにchromedriverを置きました。
AWS
1ec2-user:/usr/local/bin $ sudo wget https://chromedriver.storage.googleapis.com/78.0.3904.70/chromedriver_linux64.zip 2 3 4--2019-11-21 10:29:02-- https://chromedriver.storage.googleapis.com/78.0.3904.70/chromedriver_linux64.zip 5Resolving chromedriver.storage.googleapis.com (chromedriver.storage.googleapis.com)... 172.217.26.16, 2404:6800:4004:809::2010 6Connecting to chromedriver.storage.googleapis.com (chromedriver.storage.googleapis.com)|172.217.26.16|:443... connected. 7HTTP request sent, awaiting response... 200 OK 8Length: 5527671 (5.3M) [application/zip] 9Saving to: ‘chromedriver_linux64.zip’ 10 11chromedriver_linux64.zip 100%[=======================================>] 5.27M --.-KB/s in 0.08s 12(62.9 MB/s) - ‘chromedriver_linux64.zip’ saved [5527671/5527671]
AWS
1ec2-user:/usr/local/bin $ sudo unzip chromedriver_linux64.zip 2Archive: chromedriver_linux64.zip 3 inflating: chromedriver
AWS
1ec2-user:/usr/local/bin $ sudo rm -rf chromedriver_linux64.zip
ここまでは、https://teratail.com/questions/223722における、otnさんのおかげです。どうもありがとうございます。
追加の質問
google-chrome-stableとは、何でしょうか。googleでgoogle-chrome-stableとは、と検索してもわかりやすい説明が見つからなかったので、詳しい方がいらっしゃったらお願いします。
ちなみに、自分の場合は、前提としてで書いたように、wgetしたchromedriverのバージョンがchrome 78系だったので、google-chrome-stableにも、versionが78.というように書かれていると解釈しています。
AWS
1ec2-user:/usr/local/bin $ yum info google-chrome-stable 2Loaded plugins: priorities, update-motd, upgrade-helper 31065 packages excluded due to repository priority protections 4Installed Packages 5Name : google-chrome-stable 6Arch : x86_64 7Version : 78.0.3904.97 8Release : 1 9Size : 214 M 10Repo : installed 11Summary : Google Chrome 12URL : https://chrome.google.com/ 13License : Multiple, see https://chrome.google.com/ 14Description : The web browser from Google 15 : 16 : Google Chrome is a browser that combines a minimal design with sophisticated technology to 17 : make the web faster, safer, and easier. 18 19Available Packages 20Name : google-chrome-stable 21Arch : x86_64 22Version : 78.0.3904.108 23Release : 1 24Size : 61 M 25Repo : google-chrome 26Summary : Google Chrome 27URL : https://chrome.google.com/ 28License : Multiple, see https://chrome.google.com/ 29Description : The web browser from Google 30 : 31 : Google Chrome is a browser that combines a minimal design with sophisticated technology to 32 : make the web faster, safer, and easier.
which google-chrome-stableでもディレクトリが見つからなかったので、google-chrome-stableのディレクトリを見つける方法がわかりましたら、お願いします。
長くなってしまいましたが、どうぞよろしくお願いします。