【问题标题】:Selenium crontab scriptSelenium crontab 脚本
【发布时间】:2019-12-21 04:35:12
【问题描述】:

我有一个简单的 Selenium Python 脚本(我知道不用 Selenium 也可以完成这个特定任务,但这只是一个示例)

from selenium import webdriver
driver = webdriver.Chrome("./lib/chromedriver")
options = webdriver.ChromeOptions()
options.add_argument("headless")
driver.get("https://www.xe.com/currencyconverter/convert/?Amount=1&From=ISK&To=" + currency.upper())
exchange_rate = driver.find_element_by_xpath('//*[@class="converterresult-toAmount"]').text
driver.close()
print("Exchange rate updated: ", exchange_rate)

当我在终端中简单地运行它时,作为用户,我会看到以下输出:

Exchange rate updated:  0.00724094

我的 crontab 看起来像这样:

# m h  dom mon dow   command
SHELL=/bin/bash
PATH=/usr/local/bin/:/usr/bin:/usr/sbin

*/5 * * * *  DISPLAY=:0 python3 /path/to/my_script.py >  /path/to/logs.txt

我加了DISPLAY=:0per this answer

日志文件每 5 分钟更新一次,但始终为空,这让我相信我的 Selenium 脚本运行不正确

编辑:我做了以下更改: 到我的脚本:

options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
options.add_argument("--headless")
options.add_argument("--disable-dev-shm-usage")
driver = webdriver.Chrome(PATH + "/lib/chromedriver", chrome_options=options)
driver.get("https://www.xe.com/currencyconverter/convert/?Amount=1&From=ISK&To=" + currency.upper())
exchange_rate = driver.find_element_by_xpath('//*[@class="converterresult-toAmount"]').text
driver.close()
print("Exchange rate updated: ", exchange_rate)

到 crontab:

SHELL=/bin/bash
PATH=/usr/local/bin/:/usr/bin:/usr/sbin
*/5 * * * *  DISPLAY=:0 /usr/bin/python3 /path/to/my_script.py > /path/to/logs.txt 2>&1

现在我在logs.txt 文件中有这个错误

Traceback (most recent call last):
  File "path/to/my_script.py", line 90, in <module>
    update_exchange_rate()
  File "path/to/my_script.py", line 69, in update_exchange_rate
    body = {"values": [[get_exchange_rate(currency=currency)]]}
  File "path/to/my_script.py", line 82, in get_exchange_rate
    driver = webdriver.Chrome("/path/to/lib/chromedriver", options=options)
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.5/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/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)

【问题讨论】:

  • 在 crontab 中设置环境变量是一种尴尬的方法 - 添加 python3 的完整路径,如 /usr/bin/python3
  • @ipaleka 我试过*/5 * * * * DISPLAY=:0 /usr/bin/python3 /path/to/my_script.py &gt; /path/to/logs.txt 但还是不行:/
  • 也尝试使用./lib/chromedriver 的绝对路径。
  • @ipaleka 仍然没有运气
  • 在脚本中,options.add_argument("--headless") 和另一行 options.add_argument("--disable-extensions")

标签: python selenium selenium-webdriver cron


【解决方案1】:

对我来说,添加这个 options.binary_location = "/opt/google/chrome/chrome" 作为选项对象的一部分后它起作用了

options.add_argument('--ignore-certificate-errors')
options.add_argument('--incognito')
options.add_argument('--headless')
options.add_argument("--no-sandbox")
options.add_argument('--disable-dev-shm-usage')
options.add_argument("--disable-setuid-sandbox")
options.add_argument('--remote-debugging-port=9222')
options.binary_location = "/opt/google/chrome/chrome"

【讨论】:

    【解决方案2】:

    出于测试目的,我将 currency.upper() 更改为“USD”。还更改了您的代码,因为在初始化期间应将选项传递给驱动程序。

    from selenium import webdriver
    options = webdriver.ChromeOptions()
    options.add_argument("--headless")
    driver = webdriver.Chrome("/path/to/chromedriver", options=options)
    driver.get("https://www.xe.com/currencyconverter/convert/?Amount=1&From=ISK&To=" + "USD")
    exchange_rate = driver.find_element_by_xpath('//*[@class="converterresult-toAmount"]').text
    driver.close()
    print("Exchange rate updated: ", exchange_rate)
    

    crontab:

    */5 * * * *  DISPLAY=:0 /usr/bin/python3 /path/to/my_script.py > /path/to/logs.txt 2>&1
    

    输出:

    $ cat /path/to/logs.txt
    Exchange rate updated:  0.00806602
    

    【讨论】:

    • 不知何故它仍然不起作用 - 我也在两台不同的计算机上尝试过,但没有成功
    • 从哪里导入currency?使用更改后的 cron 行(添加“2>&1”),以便将任何错误保存到日志文件中。在此处发布错误。
    • 货币存储在一个全局变量中(当我在 cron 之外运行它时它可以工作)CURRENCIES = ["eur", "usd", "gbp"] -> currency = CURRENCIES[0]
    • Tldr;我认为我做错了在这里导致错误:chrome/webdriver.py", line 81, in __init__ desired_capabilities=desired_capabilities)
    • 不知道,真的,这个答案中提供的示例代码在我的电脑上完美运行。
    猜你喜欢
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    • 2017-05-12
    • 2013-08-30
    • 2017-01-01
    • 1970-01-01
    相关资源
    最近更新 更多