【Selenium】geckodriverを入れて動くようにする Mac| 忘備録

Python

MacでSeleniumをインストールしてもそのまま使えなかったのでメモ。

まずはSeleniumをインストール

pip3 install selenium

Seleniumの実行

from selenium import webdriver
browser = webdriver.Firefox()

すると以下のようなエラー

---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
/anaconda3/lib/python3.6/site-packages/selenium/webdriver/common/service.py in start(self)
     75                                             stderr=self.log_file,
---> 76                                             stdin=PIPE)
     77         except TypeError:

/anaconda3/lib/python3.6/subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors)
    708                                 errread, errwrite,
--> 709                                 restore_signals, start_new_session)
    710         except:

/anaconda3/lib/python3.6/subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, restore_signals, start_new_session)
   1343                             err_msg += ': ' + repr(err_filename)
-> 1344                     raise child_exception_type(errno_num, err_msg, err_filename)
   1345                 raise child_exception_type(err_msg)

FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver': 'geckodriver'

During handling of the above exception, another exception occurred:

WebDriverException                        Traceback (most recent call last)
 in ()
----> 1 browser = webdriver.Firefox()

/anaconda3/lib/python3.6/site-packages/selenium/webdriver/firefox/webdriver.py in __init__(self, firefox_profile, firefox_binary, timeout, capabilities, proxy, executable_path, options, service_log_path, firefox_options, service_args, desired_capabilities, log_path)
    155                 service_args=service_args,
    156                 log_path=service_log_path)
--> 157             self.service.start()
    158 
    159             capabilities.update(options.to_capabilities())

/anaconda3/lib/python3.6/site-packages/selenium/webdriver/common/service.py in start(self)
     81                 raise WebDriverException(
     82                     "'%s' executable needs to be in PATH. %s" % (
---> 83                         os.path.basename(self.path), self.start_error_message)
     84                 )
     85             elif err.errno == errno.EACCES:

WebDriverException: Message: 'geckodriver' executable needs to be in PATH. 
スポンサーリンク

geckodriverがない!!と怒られたのでgeckodriverを入れてみる。

行った手順
1. geckodriverのダウンロード
https://github.com/mozilla/geckodriver/releases

  1. /usr/local/binにgeckodriverをコピーする

コピーが完了したら再度以下のコードを実行

from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://www.nikkei.com')

Firefoxが立ち上がって日経経済新聞のサイトが表示されたらインストール完了!!

コメント