ものづくりのブログ

うちのネコを題材にしたものづくりができたらいいなと思っていろいろ奮闘してます。

cron から python で作った selenium のコードを実行したら動かなかった時の話...

cron の定期実行で pythonで作った selenium のコードを動かしてみたら、以下のようなエラーが出て処理が失敗してしまいました。

症状

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/Selenium/webdriver/common/service.py", line 76, in start
    stdin=PIPE)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 1344, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'geckodriver': 'geckodriver'

During handling of the above exception, another exception occurred:

"No such file or directory: 'geckodriver': 'geckodriver'"...
別に「geckodriver」がないわけではない。

$ which geckodriver
/usr/local/bin/geckodriver

cron から実行すると「geckodriver」のパスが見つからないと思われる。
今回はパスを以下のように設定(「executable_path」追加)して対応する。

    self.driver = webdriver.Firefox(
        firefox_profile=fp, 
        firefox_options=options,
        executable_path=r'/usr/local/bin/geckodriver'
    )