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

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

新規登録して質問してみよう
ただいま回答率
85.48%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

selenium

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

Q&A

2回答

3588閲覧

seleniumでchromeを動かしたい chrome driverがうまく動かない

mush01

総合スコア14

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

selenium

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

0グッド

2クリップ

投稿2019/05/20 15:54

編集2019/05/21 04:17

Seleniumでchromeを動かしたいのですが、うまくいきません。
おそらく、chrome driverの設定がうまくできていないことが原因だと思います。
safariはseleniumで操作することはできました。
環境
MacOS Mojave version 10.14.4
Chrome version = 74.0.3729.157
Chrome driver version = 74.0.3729.6
仮想環境を作ってその中で動かしています。
実行したコード

python

1import time 2from selenium import webdriver 3 4driver = webdriver.Chrome('/Users/aaa/scraping') 5driver.get('https://www.google.com/') 6time.sleep(5) 7search_box = driver.find_element_by_name("q") 8search_box.send_keys('ChromeDriver') 9search_box.submit() 10time.sleep(5) 11driver.quit()

エラー

python

1Traceback (most recent call last): 2 File "/Users/aaa/scraping/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 76, in start 3 stdin=PIPE) 4 File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 775, in __init__ 5 restore_signals, start_new_session) 6 File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/subprocess.py", line 1522, in _execute_child 7 raise child_exception_type(errno_num, err_msg, err_filename) 8PermissionError: [Errno 13] Permission denied: '/Users/aaa/scraping' 9 10During handling of the above exception, another exception occurred: 11 12Traceback (most recent call last): 13 File "a.py", line 4, in <module> 14 driver = webdriver.Chrome('/Users/aaa/scraping') 15 File "/Users/aaa/scraping/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__ 16 self.service.start() 17 File "/Users/aaa/scraping/lib/python3.7/site-packages/selenium/webdriver/common/service.py", line 88, in start 18 os.path.basename(self.path), self.start_error_message) 19selenium.common.exceptions.WebDriverException: Message: 'scraping' executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home

パスはたぶん通っていると思います。

(scraping) aaanoMBP:scraping aaa$ export -p declare -x PATH="/Users/aaa/scraping/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/aaa/scraping" declare -x PWD="/Users/aaa/scraping"

以上です。
よろしくお願いいたします。

追記

driver = webdriver.Chrome('/Users/aaa/scraping/chromedriver')

に変更したところ

Traceback (most recent call last): File "a.py", line 4, in <module> driver = webdriver.Chrome('/Users/aaa/scraping/chromedriver') File "/Users/aaa/scraping/lib/python3.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__ desired_capabilities=desired_capabilities) File "/Users/aaa/scraping/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__ self.start_session(capabilities, browser_profile) File "/Users/aaa/scraping/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "/Users/aaa/scraping/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute self.error_handler.check_response(response) File "/Users/aaa/scraping/lib/python3.7/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: cannot find Chrome binary (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Mac OS X 10.14.4 x86_64)

というエラーになり、やはりうまくいかないです。

chromediverを開いてみたら

Only local connections are allowed. Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code. [1558411773.099][SEVERE]: bind() failed: Address already in use (48) IPv4 port not available.

と書かれており、IPv4 port not availableを検索してみたら、
https://yuichon.com/2019/03/ruby-commanderror/のBlogを見つけました。
もしかして自分も適当にchrome driverを配置した原因で、binがおかしくなったのかなと思いましたが、
ターミナルでpython -Vはしっかりと動きました。

IPv4 port not availableは関係あるでしょうか?

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

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

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

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

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

can110

2019/05/21 04:22

実行ファイルchromedriverのフルパスを明記ください。
mush01

2019/05/21 04:35

/Users/aaa/scraping/chromedriver だと思います。
can110

2019/05/21 04:38

mac触ったことないのですが、「思う」のではなく、そこにファイルがあるという確証を得る方法はないのでしょうか?
mush01

2019/05/21 04:44

pythonで、 >>> import os >>> os.getcwd() '/Users/aaa/scraping' >>> os.path.abspath('chromedriver') '/Users/aaa/scraping/chromedriver' と入力して確認しました。 「思う」と書いたのは、絶対パスについて自分が勘違いしているか不安があったからです。 言い方が悪くて申し訳ありませんでした。
can110

2019/05/21 07:17

その方法だと、単に絶対パスを作りこんでいるだけでなので、回答追記のように実行ファイルの存在確認をしてください。
mush01

2019/05/21 10:39

返信有難うございます。 import os path = '/Users/aaa/scraping/chromedriver' print( os.path.isfile(path)) 以下のコードを実行したらTrueになりました。 それから、PhantomJSをchromedriverと同じディレクトリに配置し、 PhantomJSに変えて実行したら、ちゃんと動いたので、やはり、chromedriverに何かしら問題があるではないかと考えています。
can110

2019/05/21 22:28

追記の「cannot find Chrome binary」エラーより、Chromeブラウザを探しきれていないようです。 Google Chrome(と念のためChromeDriver)の再インストールを試してみてはいかがでしょうか?
guest

回答2

0

前提条件の確認

仮想環境を作ってその中で動かしています。

仮想環境とは何のことですか? Google Chrome はその仮想環境にもインストールしてありますか? 作業はすべて仮想環境で行いましたか?

ドキュメントに従う

エラーメッセージのいうようにドキュメントを読んでやり直してみてはいかがでしょうか。

Getting started with ChromeDriver on Desktop https://sites.google.com/a/chromium.org/chromedriver/getting-started より

Ensure Chromium/Google Chrome is installed in a recognized location

Chrome がデフォルトの場所にインストールされているか確かめる。

Download the ChromeDriver binary for your platform under the downloads section of this site

プラットフォームにあった ChromeDriver 実行ファイルをダウンロードする。

Help WebDriver find the downloaded ChromeDriver executable

WebDriver が ChromeDriver 実行ファイルを見つけられるようにする(webdriver.Chrome() の引数を修正したのでこれはできているはず)。

ほかにも一通り目を通したらいいと思います。

以上の手順とは関係ないが、Address already in use について

[1558677843.232][SEVERE]: bind() failed: Address already in use (48) IPv4 port not available. Exiting...

のようなメッセージが出て chromedriver がすぐ終了してしまった場合は TCP 9515 ポートがすでに使われています。別のところで chromedriver を実行していてそれが終了されていないとかの場合はそれらの chromedriver を終了してください。
netstat -f inet -anv コマンドで 127.0.0.1.9515 を LISTEN しているプロセスがいないことを確認してください。

Active Internet connections (including servers) Proto Recv-Q Send-Q Local Address Foreign Address (state) rhiwat shiwat pid epid tcp4 0 0 127.0.0.1.9515 *.* LISTEN 131072 131072 1591 0

投稿2019/05/24 06:27

criticabug

総合スコア71

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

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

0

driver = webdriver.Chrome('/Users/aaa/scraping')

提示エラーPermissionError: [Errno 13] Permission denied:より推測すると
このパスはChromeDriverのあるディレクトリのパスではないでしょうか?

chromedriverのパスが不明なので確実なことは云えませんが、たとえば/Users/aaa/scraping/bin/chromedriverのように実行ファイル名のフルパスを渡してください。

なお、ファイルの存在確認は以下のようなコードでできます。

Python

1import os 2path = '/path/to/chromedriver' 3print( os.path.isfile(path))

投稿2019/05/20 20:55

編集2019/05/21 07:15
can110

総合スコア38262

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問