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は関係あるでしょうか?