質問をすることでしか得られない、回答やアドバイスがある。

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

新規登録して質問してみよう
ただいま回答率
85.50%
Windows 10

Windows 10は、マイクロソフト社がリリースしたOSです。Modern UIを標準画面にした8.1から、10では再びデスクトップ主体に戻され、UIも変更されています。PCやスマホ、タブレットなど様々なデバイスに幅広く対応していることが特徴です。

Chrome

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

Python 3.x

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

selenium

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

Q&A

解決済

1回答

2422閲覧

Windows10 Python3.6 Celenium 環境でheadless chromeが起動しない。

sunafu

総合スコア7

Windows 10

Windows 10は、マイクロソフト社がリリースしたOSです。Modern UIを標準画面にした8.1から、10では再びデスクトップ主体に戻され、UIも変更されています。PCやスマホ、タブレットなど様々なデバイスに幅広く対応していることが特徴です。

Chrome

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

Python 3.x

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

selenium

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

0グッド

0クリップ

投稿2018/03/03 13:32

編集2018/03/04 02:06

前提・実現したいこと

headlessブラウザを起動してスクレイピングをする。

よろしくお願いします。

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

3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)]
Traceback (most recent call last):
File "test.py", line 23, in <module>
main()
File "test.py", line 16, in main
driver = webdriver.Chrome(chrome_options=options, executable_path=executable_path, service_args=["--verbose", f"--log-path={log_path}"])
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 75, in init
desired_capabilities=desired_capabilities)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 154, in init
self.start_session(desired_capabilities, browser_profile)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 243, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "C:\ProgramData\Anaconda3\lib\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
(Driver info: chromedriver=2.9.248315,platform=Windows NT 6.3 x86_64)

【Chrome Driverのデバックログの内容】
[2.995][INFO]: COMMAND InitSession {
"capabilities": {
"alwaysMatch": {
"browserName": "chrome",
"goog:chromeOptions": {
"args": [ "--no-sandbox", "--disable-gpu", "--headless" ],
"extensions": [ ]
},
"platformName": "any"
},
"firstMatch": [ {

} ]
},
"desiredCapabilities": {
"browserName": "chrome",
"goog:chromeOptions": {
"args": [ "--no-sandbox", "--disable-gpu", "--headless" ],
"extensions": [ ]
},
"platform": "ANY",
"version": ""
}
}
[2.998][INFO]: Populating Preferences file: {
"alternate_error_pages": {
"enabled": false
},
"autofill": {
"enabled": false
},
"browser": {
"check_default_browser": false
},
"distribution": {
"import_bookmarks": false,
"import_history": false,
"import_search_engine": false,
"make_chrome_default_for_user": false,
"show_welcome_page": false,
"skip_first_run_ui": true
},
"dns_prefetching": {
"enabled": false
},
"profile": {
"content_settings": {
"pattern_pairs": {
"https://,": {
"media-stream": {
"audio": "Default",
"video": "Default"
}
}
}
},
"default_content_settings": {
"geolocation": 1,
"mouselock": 1,
"notifications": 1,
"popups": 1,
"ppapi-broker": 1
},
"password_manager_enabled": false
},
"safebrowsing": {
"enabled": false
},
"search": {
"suggest_enabled": false
},
"translate": {
"enabled": false
}
}
[3.000][INFO]: Populating Local State file: {
"background_mode": {
"enabled": false
},
"ssl": {
"rev_checking": {
"enabled": false
}
}
[3.512][INFO]: RESPONSE InitSession unknown error: chrome failed to start
[3.512][DEBUG]: Log type 'driver' lost 0 entries on destruction
[3.512][DEBUG]: Log type 'browser' lost 0 entries on destruction

該当のソースコード

-- coding: utf-8 --

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import sys

def main():
print(sys.version)
options = Options()
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
#options.add_argument('--headless')
options.add_argument('--incognito')
options.add_argument('--disable-extensions')
log_path = r"C:\selenium\chromedriver.log"
executable_path = 'C:\selenium\chromedriver.exe'
driver = webdriver.Chrome(chrome_options=options, executable_path=executable_path, service_args=["--verbose", f"--log-path={log_path}"])

driver.get('https://www.google.co.jp') driver.save_screenshot('screen.png')

if name == "main":
main()

試したこと

【pathの登録】
C:\selenium\

【pythonの再インストール、バージョンの変更】
python2.xに変更 → 変化なし
python3.xを再インストール → 変化なし
python3.x 32bit を Anaconda python3.x 64bitに変更 → 変化なし

【//を/に】
『C:\Program Files (x86)\』などの//を
『C:\Program Files (x86)\』に変更
→ エラーに変化なし

【同じようなエラーを検索して解決策を実行】
https://code.i-harness.com/ja/q/2680e29

【起動オプションの変更 ※以下に変更】変化なし
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
#options.add_argument('--headless')
options.add_argument('--incognito')
options.add_argument('--disable-extensions')

補足情報(FW/ツールのバージョンなど)

【バージョン】
windows10 Pro 64bit
chrome : 64.0.3282.186(Official Build) (64 ビット)
python : 3.6.4 Anaconda [MSC v.1900 64 bit (AMD64)]
chromeWebdriver : ChromeDriver 2.35(Supports Chrome v62-64) → chromedriver_win32

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

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

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

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

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

guest

回答1

0

ベストアンサー

options.binary_location = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"

binary_location の行を削除して実行しても同じような問題が発生しますか?

Python

1# -*- coding: utf-8 -*- 2from selenium import webdriver 3from selenium.webdriver.chrome.options import Options 4import sys 5 6 7def main(): 8 print(sys.version) 9 options = Options() 10 options.add_argument('--headless') 11 driver = webdriver.Chrome(chrome_options=options, executable_path=r"C:\selenium\chromedriver.exe") 12 13 driver.get('https://www.google.co.jp') 14  # ヘッドレスモードはUIを表示しないため、スクリーンショットで動作確認。 15 driver.save_screenshot('screen.png') 16 17 18if __name__ == "__main__": 19 main() 20 21

こちらの実行環境も書いておきます。

Chrome バージョン: 64.0.3282.186(Official Build) (64 ビット)
Chrome Driver 2.35
python 3.6.3 |Anaconda custom (64-bit)| (default, Oct 15 2017, 03:27:45) [MSC v.1900 64 bit (AMD64)]

■フォルダ構成

txt

1C:\selenium\ 2 |- chromedriver.exe 3

Chrome Driver 2.35のダウンロード先


Chrome Driverのデバックログの出力方法

Python

1log_path = r"C:\selenium\qc1.log" 2driver = webdriver.Chrome(r'C:\selenium\chromedriver', service_args=["--verbose", f"--log-path={log_path}"])

上記出力されたデバックログのLaunching chrome:以前の行にメッセージなどは出てませんでしょうか?
あと--headless オプションなしでも同じエラーになるのでしょうか?


こちらの環境のLaunching chrome:行以前のchromedriver.logです。

Json

1[1520116785.758][INFO]: COMMAND InitSession { 2 "capabilities": { 3 "alwaysMatch": { 4 "browserName": "chrome", 5 "goog:chromeOptions": { 6 "args": [ "--no-sandbox", "--disable-gpu", "--headless" ], 7 "extensions": [ ] 8 }, 9 "platformName": "any" 10 }, 11 "firstMatch": [ { 12 13 } ] 14 }, 15 "desiredCapabilities": { 16 "browserName": "chrome", 17 "goog:chromeOptions": { 18 "args": [ "--no-sandbox", "--disable-gpu", "--headless" ], 19 "extensions": [ ] 20 }, 21 "platform": "ANY", 22 "version": "" 23 } 24} 25[1520116785.761][INFO]: Populating Preferences file: { 26 "alternate_error_pages": { 27 "enabled": false 28 }, 29 "autofill": { 30 "enabled": false 31 }, 32 "browser": { 33 "check_default_browser": false 34 }, 35 "distribution": { 36 "import_bookmarks": false, 37 "import_history": false, 38 "import_search_engine": false, 39 "make_chrome_default_for_user": false, 40 "skip_first_run_ui": true 41 }, 42 "dns_prefetching": { 43 "enabled": false 44 }, 45 "profile": { 46 "content_settings": { 47 "pattern_pairs": { 48 "https://*,*": { 49 "media-stream": { 50 "audio": "Default", 51 "video": "Default" 52 } 53 } 54 } 55 }, 56 "default_content_setting_values": { 57 "geolocation": 1 58 }, 59 "default_content_settings": { 60 "geolocation": 1, 61 "mouselock": 1, 62 "notifications": 1, 63 "popups": 1, 64 "ppapi-broker": 1 65 }, 66 "password_manager_enabled": false 67 }, 68 "safebrowsing": { 69 "enabled": false 70 }, 71 "search": { 72 "suggest_enabled": false 73 }, 74 "translate": { 75 "enabled": false 76 } 77} 78[1520116785.764][INFO]: Populating Local State file: { 79 "background_mode": { 80 "enabled": false 81 }, 82 "ssl": { 83 "rev_checking": { 84 "enabled": false 85 } 86 } 87}

Python

1# -*- coding: utf-8 -*- 2from selenium import webdriver 3from selenium.webdriver.chrome.options import Options 4import sys 5 6 7def main(): 8 print(sys.version) 9 options = Options() 10 options.add_argument('--no-sandbox') 11 options.add_argument('--disable-gpu') 12 options.add_argument('--headless') 13 log_path = r"C:\selenium\chromedriver.log" 14 executable_path = 'C:\selenium\chromedriver.exe' 15 driver = webdriver.Chrome(chrome_options=options, executable_path=executable_path, service_args=["--verbose", f"--log-path={log_path}"]) 16 17 driver.get('https://www.google.co.jp') 18 driver.save_screenshot('screen.png') 19 20 21if __name__ == "__main__": 22 main()

うーむ。同じログですかー。そうなると起動オプションの違いでしょうか。
こちらの起動オプションは

txt

1[1520118045.361][INFO]: Launching chrome: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --disable-background-networking --disable-client-side-phishing-detection --disable-default-apps --disable-gpu --disable-hang-monitor --disable-popup-blocking --disable-prompt-on-repost --disable-sync --disable-web-resources --enable-automation --enable-logging --force-fieldtrials=SiteIsolationExtensions/Control --headless --ignore-certificate-errors --load-extension="C:\Users\ユーザー名\AppData\Local\Temp\scoped_dir112260_27266\internal" --log-level=0 --metrics-recording-only --no-first-run --no-sandbox --password-store=basic --remote-debugging-port=12303 --test-type=webdriver --use-mock-keychain --user-data-dir="C:\Users\ユーザー名\AppData\Local\Temp\scoped_dir112260_30968" data:,

1, ログメッセージにchrome failed to startのメッセージは出力されているのでしょうか?
2, Proxyの設定をされていますか?こちらは特にProxyは使っていません。


オプションの纏めありがとうございました。
あと考えられる点としては、最小構成でChromeを起動してみるぐらいかと。

オプション内容
--incognitoシークレットモードでChromeを起動
--disable-extensions拡張機能を無効化

Python

1 options.add_argument('--no-sandbox') 2 options.add_argument('--disable-gpu') 3 #options.add_argument('--headless') 4 options.add_argument('--incognito') 5 options.add_argument('--disable-extensions')

これで無理でしたら、質問文を現在のコードに改訂して他の人の回答を待ったほうが良いかもしれません。

投稿2018/03/03 19:59

編集2018/03/04 01:42
umyu

総合スコア5846

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

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

sunafu

2018/03/03 20:13

ご回答ありがとうございます! ----- options.binary_location = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" ---- を削除してもエラー内容は変わりませんでした。 貼ってくださったコードの『 driver = webdriver.Chrome(chrome_options=options, executable_path=r"C:\selenium\chromedriver.exe")』の最後の部分を『C:\Python36\selenium\webdriver\chromedriver.exe』に変えて実行したところ、 --------- 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] Traceback (most recent call last): File "test.py", line 18, in <module> main() File "test.py", line 11, in main driver = webdriver.Chrome(chrome_options=options, executable_path=r"C:\Python36\selenium\webdriver\chromedriver.exe") File "C:\Python36\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 75, in __init__ desired_capabilities=desired_capabilities) File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 154, in __init__ self.start_session(desired_capabilities, browser_profile) File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 243, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "C:\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute self.error_handler.check_response(response) File "C:\Python36\lib\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 (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.16299 x86_64) -------- こういった内容のログが流れました。
umyu

2018/03/03 20:18

うーん、こちらの環境では再現しないですね。 options.add_argument('--no-sandbox') options.add_argument('--disable-gpu') この2つのオプションを追加してみても、同じメッセージでしょうか?
sunafu

2018/03/03 20:21

お返事ありがとうございます! options = Options() options.add_argument('--no-sandbox') options.add_argument('--disable-gpu') options.add_argument('--headless') driver = webdriver.Chrome(chrome_options=options, executable_path="C:\Python36\selenium\webdriver\chromedriver.exe") としたのですが、変わりません。。。
sunafu

2018/03/03 20:27

ありがとうございます! 参考にさせて頂きます┏○
umyu

2018/03/03 20:30

> 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)] これ32bitのpythonをインストールしてませんか?
sunafu

2018/03/03 20:32

Python 64bitでやって同じエラーが出てたので、ChromewebDriverに合わせて32bitにしてみたのですが、64bitにしてもう一度やってみます! 今環境を同じにするため、Anacondaから64bitのをインストール中です!
sunafu

2018/03/03 20:48

環境を変えてみたのですが。 ----エラー------ 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)] Traceback (most recent call last): File "test.py", line 21, in <module> main() File "test.py", line 14, in main driver = webdriver.Chrome(chrome_options=options, executable_path=r"C:\Python36\selenium\webdriver\chromedriver.exe") File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 75, in __init__ desired_capabilities=desired_capabilities) File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 154, in __init__ self.start_session(desired_capabilities, browser_profile) File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 243, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute self.error_handler.check_response(response) File "C:\ProgramData\Anaconda3\lib\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 (Driver info: chromedriver=2.35.528161 (5b82f2d2aae0ca24b877009200ced9065a772e73),platform=Windows NT 10.0.16299 x86_64) ----- ---コード---- # -*- coding: utf-8 -*- from selenium import webdriver from selenium.webdriver.chrome.options import Options import sys def main(): print(sys.version) options = Options() options.add_argument('--no-sandbox') options.add_argument('--disable-gpu') options.add_argument('--headless') driver = webdriver.Chrome(chrome_options=options, executable_path=r"C:\Python36\selenium\webdriver\chromedriver.exe") driver.get('https://www.google.co.jp') driver.save_screenshot('screen.png') if __name__ == "__main__": main() ------ この状態のままです。┏○))
sunafu

2018/03/03 21:59

ありがとうございます! --headless オプションなしでも同じエラーになります! ログのメッセージは下記になります。 ----- "browser": { "check_default_browser": false }, "distribution": { "import_bookmarks": false, "import_history": false, "import_search_engine": false, "make_chrome_default_for_user": false, "show_welcome_page": false, "skip_first_run_ui": true }, "dns_prefetching": { "enabled": false }, "profile": { "content_settings": { "pattern_pairs": { "https://*,*": { "media-stream": { "audio": "Default", "video": "Default" } } } }, "default_content_settings": { "geolocation": 1, "mouselock": 1, "notifications": 1, "popups": 1, "ppapi-broker": 1 }, "password_manager_enabled": false }, "safebrowsing": { "enabled": false }, "search": { "suggest_enabled": false }, "translate": { "enabled": false } } [2.998][INFO]: Populating Local State file: { "background_mode": { "enabled": false }, "ssl": { "rev_checking": { "enabled": false } } }
sunafu

2018/03/03 22:53

こちらもログを取ってみたところ同じ内容でした。 ------ [2.995][INFO]: COMMAND InitSession { "capabilities": { "alwaysMatch": { "browserName": "chrome", "goog:chromeOptions": { "args": [ "--no-sandbox", "--disable-gpu", "--headless" ], "extensions": [ ] }, "platformName": "any" }, "firstMatch": [ { } ] }, "desiredCapabilities": { "browserName": "chrome", "goog:chromeOptions": { "args": [ "--no-sandbox", "--disable-gpu", "--headless" ], "extensions": [ ] }, "platform": "ANY", "version": "" } } [2.998][INFO]: Populating Preferences file: { "alternate_error_pages": { "enabled": false }, "autofill": { "enabled": false }, "browser": { "check_default_browser": false }, "distribution": { "import_bookmarks": false, "import_history": false, "import_search_engine": false, "make_chrome_default_for_user": false, "show_welcome_page": false, "skip_first_run_ui": true }, "dns_prefetching": { "enabled": false }, "profile": { "content_settings": { "pattern_pairs": { "https://*,*": { "media-stream": { "audio": "Default", "video": "Default" } } } }, "default_content_settings": { "geolocation": 1, "mouselock": 1, "notifications": 1, "popups": 1, "ppapi-broker": 1 }, "password_manager_enabled": false }, "safebrowsing": { "enabled": false }, "search": { "suggest_enabled": false }, "translate": { "enabled": false } } [3.000][INFO]: Populating Local State file: { "background_mode": { "enabled": false }, "ssl": { "rev_checking": { "enabled": false } }
sunafu

2018/03/03 23:13

chromeをアンインストールしてやってみたのですが、全く同じエラーが起きます。 そのあたりを少し調べています。
sunafu

2018/03/03 23:35

ログの意味するところがまだわからないのですが、調べてみます! optionの違いをまとめてみました。 ◯=同じものがある △=自分だけがある ×=自分にない ----自分のログ---- [3.040][INFO]: Launching chrome: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" ◯--disable-background-networking ◯--disable-client-side-phishing-detection △--disable-component-update ◯--disable-default-apps ◯--disable-hang-monitor ◯--disable-prompt-on-repost ◯--disable-sync ◯--disable-web-resources ◯--enable-logging △--full-memory-crash-report △--ignore-certificate-errors ◯--load-extension="C:\Users\ユーザー名\AppData\Local\Temp\scoped_dir22800_1069\internal" ◯--logging-level=1 ※値が違います。 ◯--metrics-recording-only ◯--no-first-run ◯--password-store=basic ◯--remote-debugging-port=12009 △--safebrowsing-disable-auto-update △--safebrowsing-disable-download-protection ◯--use-mock-keychain ◯--user-data-dir="C:\Users\ユーザー名\AppData\Local\Temp\scoped_dir22800_4162" data:, -------------------- ×--disable-gpu ×--disable-popup-blocking ×--enable-automation ×--force-fieldtrials=SiteIsolationExtensions/Control ×--headless --ignore-certificate-errors ×--no-sandbox ×--test-type=webdriver
sunafu

2018/03/03 23:37

あとその下にこういったログがありました。 [3.512][INFO]: RESPONSE InitSession unknown error: chrome failed to start [3.512][DEBUG]: Log type 'driver' lost 0 entries on destruction [3.512][DEBUG]: Log type 'browser' lost 0 entries on destruction
sunafu

2018/03/03 23:46

ProxyやVPNなどは使っていません。
sunafu

2018/03/04 01:51 編集

細かい説明まで本当に有難うございます。 optionを追加して試してみたのですが同じエラーのままでした。。。 umyuさんの仰る通り、一度質問文を変えてみます! ありがとうございました!┏○
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問