[英]Execute Python scripts with Selenium via Crontab
我有幾個在 Debian 服務器上使用 selenium webdriver 的 python 腳本。 如果我從終端(通常以 root 用戶)手動運行它們,一切正常,但是每次我嘗試通過 crontab 運行它們時,我都會遇到這樣的異常:
WebDriverException: Message: Can't load the profile. Profile Dir: /tmp/tmpQ4vStP If you specified a log_file in the FirefoxBinary constructor, check it for details.
試試這個腳本:
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from pyvirtualdisplay import Display
from selenium import webdriver
import datetime
import logging
FIREFOX_PATH = '/usr/bin/firefox'
if __name__ == '__main__':
cur_date = datetime.datetime.now().strftime('%Y-%m-%d')
logging.basicConfig(filename="./logs/download_{0}.log".format(cur_date),
filemode='w',
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s')
try:
display = Display(visible=0, size=(800, 600))
display.start()
print 'start'
logging.info('start')
binary = FirefoxBinary(FIREFOX_PATH,
log_file='/home/egor/dev/test/logs/firefox_binary_log.log')
driver = webdriver.Firefox()
driver.get("http://google.com")
logging.info('title: ' + driver.title)
driver.quit()
display.stop()
except:
logging.exception('')
logging.info('finish')
print 'finish'
它的 crontab 命令:
0 13 * * * cd "/home/egor/dev/test" && python test.py
此腳本的日志文件如下所示:
2016-09-27 16:30:01,742 - DEBUG - param: "['Xvfb', '-help']"
2016-09-27 16:30:01,743 - DEBUG - command: ['Xvfb', '-help']
2016-09-27 16:30:01,743 - DEBUG - joined command: Xvfb -help
2016-09-27 16:30:01,745 - DEBUG - process was started (pid=23042)
2016-09-27 16:30:01,747 - DEBUG - process has ended
2016-09-27 16:30:01,748 - DEBUG - return code=0
2016-09-27 16:30:01,748 - DEBUG - stdout=
2016-09-27 16:30:01,751 - DEBUG - param: "['Xvfb', '-br', '-nolisten', 'tcp', '-screen', '0', '800x600x24', ':1724']"
2016-09-27 16:30:01,751 - DEBUG - command: ['Xvfb', '-br', '-nolisten', 'tcp', '-screen', '0', '800x600x24', ':1724']
2016-09-27 16:30:01,751 - DEBUG - joined command: Xvfb -br -nolisten tcp -screen 0 800x600x24 :1724
2016-09-27 16:30:01,753 - DEBUG - param: "['Xvfb', '-br', '-nolisten', 'tcp', '-screen', '0', '800x600x24', ':1725']"
2016-09-27 16:30:01,753 - DEBUG - command: ['Xvfb', '-br', '-nolisten', 'tcp', '-screen', '0', '800x600x24', ':1725']
2016-09-27 16:30:01,753 - DEBUG - joined command: Xvfb -br -nolisten tcp -screen 0 800x600x24 :1725
2016-09-27 16:30:01,755 - DEBUG - process was started (pid=23043)
2016-09-27 16:30:01,755 - DEBUG - DISPLAY=:1725
2016-09-27 16:30:01,855 - INFO - start
2016-09-27 16:30:31,965 - ERROR -
Traceback (most recent call last):
File "test.py", line 31, in <module>
driver = webdriver.Firefox()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/webdriver.py", line 103, in __init__
self.binary, timeout)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/extension_connection.py", line 51, in __init__
self.binary.launch_browser(self.profile, timeout=timeout)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 68, in launch_browser
self._wait_until_connectable(timeout=timeout)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/firefox/firefox_binary.py", line 106, in _wait_until_connectable
% (self.profile.path))
WebDriverException: Message: Can't load the profile. Profile Dir: /tmp/tmpQ4vStP If you specified a log_file in the FirefoxBinary constructor, check it for details.
2016-09-27 16:30:31,966 - INFO - finish
我嘗試過的:
我真的被這個問題困住了。
我在 Debian 7.7 上有 python 2.7.10、selenium 2.53.6 和 Xvbf 和 Firefox 47.0.1
問題與環境變量有關:cron 是由系統啟動的,對用戶環境一無所知。
因此,解決問題的方法是讓cron 運行一個shell 腳本,該腳本先設置所需的環境變量,然后再運行該腳本。 在我的情況下,我需要像這樣設置PATH
變量: PATH=/root/anaconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
此外,在某些情況下設置HOME
或DISPLAY
變量可能很有用。
嘗試使用硬編碼的 Firefox 二進制文件https://seleniumhq.github.io/selenium/docs/api/py/webdriver_firefox/selenium.webdriver.firefox.firefox_binary.html
selenium.webdriver.firefox.firefox_binary.FirefoxBinary("/your/binary/location/firefox")
driver = webdriver.Firefox(firefox_binary=binary)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.