【问题标题】:Selenium - Python script works from CLI, but errors when launching with cronSelenium - Python 脚本可从 CLI 运行,但使用 cron 启动时出错
【发布时间】:2021-02-13 00:06:45
【问题描述】:

首先,我是 Python 脚本和一般编程的初学者。我想自动化 Raspberry Pi 启动,全屏打开 Chromium,转到网页,然后自动登录。基本上,非技术人员可以打开它并砰的一声,没有键盘或任何需要的东西,只是一个显示器。我已经为此工作了大约一个星期,并且学到了很多东西,但是我碰壁了,无法超越。

在带有 Raspbian 10 的 Ras Pi 4+ 上运行

已安装 Selenium 和 Chromedriver

我在 Python 中完成了 Selenium 脚本:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options


chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--start-fullscreen')
chrome_options.add_argument('user-data-dir=/home/pi/Documents/website_login/Chromium_user_data')
chrome_options.add_argument('disable-infobars')
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-gpu")

driver = webdriver.Chrome('/usr/lib/chromium-browser/chromedriver', options=chrome_options)

driver.get("https://www.mywebsite.com")

driver.implicitly_wait(10)

#driver.find_element_by_id('rcc-confirm-button').click()  #comment out because after run once the cookies banner gets saves in user settings

driver.find_element_by_link_text('Log in').click()

delay = 5

driver.find_element_by_id('ddlsubsciribers').send_keys('agency')
driver.find_element_by_id('memberfname').send_keys('user')
driver.find_element_by_id('memberpwd').send_keys('password')
driver.find_element_by_id('login').click()

当我从终端运行它时它工作正常。打开 chromium,进入页面,使用 Selenium 魔法,登录,这正是我想要的。

然后我尝试使用以下命令在 Pi 上使用 crontab -e 启动它:

@reboot sleep 20; /usr/bin/python3 /home/pi/Documents/website_login/iar_login.py > /home/pi/Documents/website_login/iar_errorlog.err >2&1

当我重新启动并在日志文件中收到以下错误消息时没有任何反应:

Traceback (most recent call last):
  File "/home/pi/Documents/website_login/iar_login.py", line 16, in <module>
    driver = webdriver.Chrome('/usr/lib/chromium-browser/chromedriver', options=chrome_options)
  File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
  (unknown error: DevToolsActivePort file doesn't exist)
  (The process started from chrome location /usr/bin/chromium-browser is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

我已经搜索并阅读了有关错误的所有内容。我认为这可能是某种类型的权限错误,但我没有想法尝试。

【问题讨论】:

    标签: python selenium cron selenium-chromedriver raspbian


    【解决方案1】:

    已修复。在 crontab 文件中添加 'export DISPLAY=:0' 修复了它。 Selenium 正在尝试执行,但找不到显示。

    【讨论】:

      猜你喜欢
      • 2016-09-07
      • 2022-09-30
      • 2011-04-14
      • 2018-01-30
      • 2019-04-16
      • 2011-05-26
      • 2011-05-28
      • 2016-04-20
      相关资源
      最近更新 更多