質問するログイン新規登録
CentOS

CentOSは、主にRed Hat Enterprise Linux(RHEL)をベースにした、フリーのソフトウェアオペレーティングシステムです。

Chrome

Google Chromeは携帯、テレビ、デスクトップなどの様々なプラットフォームで利用できるウェブブラウザです。Googleが開発したもので、Blink (レンダリングエンジン) とアプリケーションフレームワークを使用しています。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

selenium

Selenium(セレニウム)は、ブラウザをプログラムで作動させるフレームワークです。この原理を使うことにより、ブラウザのユーザーテストなどを自動化にすることができます。

Zabbix

モニタリングツール

Q&A

1回答

886閲覧

centosでseleniumからchromeを起動するとcrashする

4gre4-034

総合スコア0

CentOS

CentOSは、主にRed Hat Enterprise Linux(RHEL)をベースにした、フリーのソフトウェアオペレーティングシステムです。

Chrome

Google Chromeは携帯、テレビ、デスクトップなどの様々なプラットフォームで利用できるウェブブラウザです。Googleが開発したもので、Blink (レンダリングエンジン) とアプリケーションフレームワークを使用しています。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

selenium

Selenium(セレニウム)は、ブラウザをプログラムで作動させるフレームワークです。この原理を使うことにより、ブラウザのユーザーテストなどを自動化にすることができます。

Zabbix

モニタリングツール

0グッド

0クリップ

投稿2023/08/26 08:53

編集2023/08/26 08:54

0

0

実現したいこと

zabbixで外部チェックを使用して、WEBサイトのシナリオ監視したい

前提

項目内容備考
OSCentOS Linux release 7.9.2009 (Core)
ChromeDriver116.0.5845.96
Google Chrome116.0.5845.110
zabbixサーバzabbix_server (Zabbix) 5.0.14
seleniumselenium (3.141.0)

発生している問題・エラーメッセージ

Value of type "string" is not suitable for value type "Numeric (unsigned)". Value "Traceback (most recent call last): File "/usr/lib/zabbix/externalscripts/sample.py", line 99, in <module> login("test@hogehoge.com", "testtest") File "/usr/lib/zabbix/externalscripts/sample.py", line 39, in login browser = webdriver.Chrome(executable_path=chromedriver_path, options=options) File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__ desired_capabilities=desired_capabilities) File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__ self.start_session(capabilities, browser_profile) File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute self.error_handler.check_response(response) File "/usr/local/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: crashed. (unknown error: DevToolsActivePort file doesn't exist) (The process started from chrome location /opt/google/chrome/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

該当のソースコード

python

1#!/usr/bin/python3 2from selenium import webdriver 3from selenium.webdriver.chrome.options import Options 4from selenium.webdriver.common.by import By 5from selenium.webdriver.support.ui import WebDriverWait 6from selenium.webdriver.support import expected_conditions as EC 7import selenium.common.exceptions 8import os 9import datetime 10import sys 11 12 13chromedriver_path = '/usr/local/bin/chromedriver' 14screenshot_directory = '/usr/lib/zabbix/externalscripts/screenshot' 15 16options = webdriver.ChromeOptions() 17options.binary_location = '/opt/google/chrome/google-chrome' 18 19options.add_argument('--no-sandbox') 20options.add_argument('--headless') 21options.add_argument('--disable-extensions') 22options.add_argument('--no-sandbox') 23options.add_argument('--disable-gpu') 24options.add_argument('--disable-dev-shm-usage') 25 26 27def take_screenshot(browser, file_path): 28 browser.save_screenshot(file_path) 29 30def check_login_successful(browser, username): 31 try: 32 user_element = browser.find_element(By.XPATH, f"//a[contains(text(), '{username}')]") 33 return True 34 except selenium.common.exceptions.NoSuchElementException: 35 return False 36 37def login(username, password): 38 browser = webdriver.Chrome(executable_path=chromedriver_path, options=options) 39 browser.implicitly_wait(15) 40 41 url = "https://sample.com" 42 browser.get(url) 43 44 try: 45 username_field = WebDriverWait(browser, 30).until( 46 EC.presence_of_element_located((By.NAME, "user")) 47 ) 48 password_field = browser.find_element(By.NAME, "passwd") 49 login_button = browser.find_element(By.CSS_SELECTOR, "button.LoginSubmit") 50 51 username_field.send_keys(username) 52 password_field.send_keys(password) 53 login_button.click() 54 55 # ログイン成功かどうかを確認 56 try: 57 WebDriverWait(browser, 30).until(EC.url_changes(url)) 58 if check_login_successful(browser, username): 59 result = "OK" 60 return_code = 0 61 else: 62 result = "NG" 63 return_code = 1 64 with open("/tmp/aaa.txt", "w") as file: 65 file.write("Login failed") 66 67 except: 68 result = "NG" 69 return_code = 1 70 with open("/tmp/aaa.txt", "w") as file: 71 file.write("Login failed") 72 73 current_datetime = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") 74 screenshot_file_name = f"{screenshot_directory}/screenshot_{current_datetime}_{result}.png" 75 take_screenshot(browser, screenshot_file_name) 76 77 except Exception: 78 result = "NG" 79 return_code = 1 80 with open("/tmp/aaa.txt", "w") as file: 81 file.write("Login failed") 82 83 finally: 84 # ブラウザを終了 85 browser.quit() 86 print(return_code) 87 if result == "OK": 88 try: 89 os.remove("/tmp/aaa.txt") 90 except FileNotFoundError: 91 pass 92 sys.exit(return_code) 93 94if __name__ == "__main__": 95 login("test@hogehoge.com", "testtest") 96

試したこと

  • rootユーザで実施すると問題なし
  • zabbixユーザで実施するち当該エラーとなる
  • sudo -u zabbix DISPLAY=:99 /usr/bin/python3 sample.py

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ikedas

2023/08/26 22:15

zabbixユーザでgoogle-chromeを直接実行するとどうなりますか。
guest

回答1

0

rootでsudo実行結果は、以下です

環境変数の問題を色々調べていますが、どうもうまくいっていません
zabbixユーザは、「nologin」にしているのであまりイジりたくないのが本音です

# sudo -u zabbix DISPLAY=:99 google-chrome [11896:11896:0905/230258.188523:ERROR:ozone_platform_x11.cc(240)] Missing X server or $DISPLAY [11896:11896:0905/230258.188584:ERROR:env.cc(255)] The platform failed to initialize. Exiting.

投稿2023/09/05 14:05

4gre4-034

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.30%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問