前提・実現したいこと
webブラウザーで対象のURLを渡して、VPS上でseleniumを起動して渡されたURLのスクリーンショットを取ろうとしているのですが
エラーが出て起動しません。起動するための助言をお願いいたします。
環境
VPS(CentOS7)
nginx 1.12.2
uWSGI
python3.6
flask
ChromeDriver 2.36.540471
発生している問題・エラーメッセージ
Traceback (most recent call last): File "/usr/local/pyenv/versions/3.6.1/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app response = self.full_dispatch_request() File "/usr/local/pyenv/versions/3.6.1/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request rv = self.handle_user_exception(e) File "/usr/local/pyenv/versions/3.6.1/lib/python3.6/site-packages/flask_cors/extension.py", line 161, in wrapped_function return cors_after_request(app.make_response(f(*args, **kwargs))) File "/usr/local/pyenv/versions/3.6.1/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception reraise(exc_type, exc_value, tb) File "/usr/local/pyenv/versions/3.6.1/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise raise value File "/usr/local/pyenv/versions/3.6.1/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request rv = self.dispatch_request() File "/usr/local/pyenv/versions/3.6.1/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "./api/route_api_walls.py", line 56, in create_walls_data selenium_util = libs.SeleniumUtility(request_form_dict['url']) File "./libs/seleniumutility.py", line 35, in __init__ self.driver = webdriver.Chrome(chrome_options=opts,executable_path=self.driver_location,service_log_path=self.service_log_path) File "/usr/local/pyenv/versions/3.6.1/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 86, in __init__ desired_capabilities=desired_capabilities) File "/usr/local/pyenv/versions/3.6.1/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 155, in __init__ self.start_session(desired_capabilities, browser_profile) File "/usr/local/pyenv/versions/3.6.1/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 244, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "/usr/local/pyenv/versions/3.6.1/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 318, in execute self.error_handler.check_response(response) File "/usr/local/pyenv/versions/3.6.1/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 246, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally (Driver info: chromedriver=2.36.540471 (9c759b81a907e70363c6312294d30b6ccccc2752),platform=Linux 3.10.0-693.17.1.el7.x86_64 x86_64)
該当のソースコード
python
1# -*- coding: utf-8 -*- 2 3from __future__ import absolute_import, division, print_function 4from pyvirtualdisplay import Display 5from selenium import webdriver 6from selenium.webdriver.chrome.options import Options 7from datetime import datetime 8from PIL import Image 9import time,os 10 11class SeleniumUtility: 12 chromeservice = None 13 driver = "" 14 display = "" 15 16 screen_shot_path = "/var/www/xxxxx/xxxxxx/static/img/thumbnails/" 17 view_image_path = "/static/img/thumbnails/" 18 browser_path = '/usr/bin/google-chrome' 19 driver_location = '/usr/local/bin/chromedriver' 20 service_log_path = '/var/log/selenium_service.log' 21 22 def __init__(self, url): 23 self.display = Display(visible=0, size=(1366, 768)) 24 self.display.start() 25 26 os.environ["HOME"] = "/home/chrome" 27 28 opts = Options() 29 opts.binary_location = self.browser_path 30 opts.add_argument('--headless') 31 opts.add_argument('--disable-gpu') 32 opts.add_argument('--no-sandbox') 33 #opts.add_argument('user-data-dir=/var/www/xxxxx/xxxxxx/chrome_tmp') 34 self.driver = webdriver.Chrome(chrome_options=opts,executable_path=self.driver_location,service_log_path=self.service_log_path) 35 self.driver.get(url) 36 time.sleep(2) 37 38 def screenshot(self, screen_shot_path=None): 39 file_name = datetime.now().strftime("%Y%m%d%H%M%S") + ".png" 40 if screen_shot_path is None: 41 screen_shot_path = self.screen_shot_path + file_name 42 43 self.driver.save_screenshot(screen_shot_path) 44 45 img = Image.open(screen_shot_path, 'r') 46 img.thumbnail((455, 256), Image.ANTIALIAS) 47 img.save(self.screen_shot_path + "th_" + file_name, 'PNG', quality=100, optimize=True) 48 return self.view_image_path + "th_" + file_name 49 50 def title(self): 51 return self.driver.title 52 53 def contents(self): 54 return self.driver.page_source 55 56 def close(self): 57 self.driver.close() 58 self.driver.quit() 59 self.display.stop()
nginx
1 location / { 2 include uwsgi_params; 3 uwsgi_read_timeout 3000; 4 uwsgi_pass unix:/var/www/xxxxx/xxxxxx/xxxxxx.sock; 5 }
uwsgi
1[uwsgi] 2uid =nginx 3gid = nginx 4 5chdir = /var/www/xxxxx/xxxxxx 6 7app = index:app 8 9module = %(app) 10 11callable = app 12 13logto = /var/log/uwsgi/%n.log 14 15master = true 16processes = 5 17 18socket = xxxxxx.sock 19chmod-socket =666 20vacuum = true 21 22die-on-term = true 23
試したこと
成功
- Supervisorを立ち上げて、flaskのサーバーで試してみたところ問題なく実行できました。
- pythonのクラスを直接呼び出し試してみたところ問題なく実行できました。(sudo -u nginx class_selenium_test.py)
失敗
- nginxがhttpsで動かしていてwebdriverのlocalserverがhttpがデフォルトになっていたのでhttpsに書き換えても実行できませんでした。
- Xvfbをpyvirtualdisplayではなく常にバックグラウンドで動かしDISPLAY番号をexportしても実行できませんでした。
- webdriverをRemote経由で呼び出しても実行できませんでした。ただし、クラス直接呼び出しだとをChomeで直接呼び出した時と同様に実行できました。
補足情報(FW/ツールのバージョンなど)
- nginx経由だとlocalhostが見つけられてない感じがします。
- SELinuxはdisabledにしています。
- Supervisorはhttpですが、nginxはhttpsで動かしています。
class_selenium_test.py
python
1import sys,os 2sys.path.append(os.pardir) 3import libs 4 5selenium =libs.SeleniumUtility('https://www.google.co.jp/') 6print(selenium.title()) 7selenium.close()
https://ja.stackoverflow.com/questions/43191/centos7nginxuwsgiflaskselenium%E3%81%A7chromedriver%E3%81%8C%E7%AB%8B%E3%81%A1%E4%B8%8A%E3%81%8C%E3%82%89%E3%81%AA%E3%81%84
とマルチポストになります。
解決策に関しましては、相互に記述しますので
ご容赦ください。

あなたの回答
tips
プレビュー