【发布时间】: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 > /path/to/logs.txt但还是不行:/ -
也尝试使用
./lib/chromedriver的绝对路径。 -
@ipaleka 仍然没有运气
-
在脚本中,
options.add_argument("--headless")和另一行options.add_argument("--disable-extensions")。
标签: python selenium selenium-webdriver cron