I have this code I've been working on:
def setup_browser():
options = Options()
#options.add_argument("--headless")
options.add_argument('--disable-extensions')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument(
"--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36")
options.add_argument(
"--user-data-dir=/home/ubuntu/snap/chromium/common/chromium/")
options.add_argument("--profile-directory=Profile 1")
#options.add_argument("--window-size=1920,1080")
driver = webdriver.Chrome(options=options)
return driver
Then I created a batch script (rate_trips.sh) to run it periodically through cron (as user), which is as follows:
pkill chromium
pkill chromium-browser
pkill chromedriver
pkill chrome
cd /home/ubuntu/Documents/turo
. venv/bin/activate
python -c "from auto import rate_all_trips;rate_all_trips()"
The rate_all_trips function calls the setup_browser, but I'll omit it for now since I don't think the problem is related to that part.
The log then returns this error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/ubuntu/Documents/turo/auto.py", line 108, in rate_all_trips
driver = setup_browser()
File "/home/ubuntu/Documents/turo/auto.py", line 31, in setup_browser
driver = webdriver.Chrome(options=options)
File "/home/ubuntu/Documents/turo/venv/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 76, in __init__
RemoteWebDriver.__init__(
File "/home/ubuntu/Documents/turo/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/home/ubuntu/Documents/turo/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/ubuntu/Documents/turo/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/ubuntu/Documents/turo/venv/lib/python3.8/site-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 /snap/chromium/2198/usr/lib/chromium-browser/chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
It works perfectly when running the script manually (just typing ./rate_trips.sh).
For reference, the crontab -e is as follows:
* * * * * /home/ubuntu/rate_trips.sh > /home/ubuntu/rate_trips.log 2>&1
Any help would be greatly appreciated, thank you so much.
Tried running a selenium script through cron but it didn't work.
Edit: Adding options.add_argument('--remote-debugging-port=9222') changed the output to:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/ubuntu/Documents/turo/auto.py", line 109, in rate_all_trips
driver = setup_browser()
File "/home/ubuntu/Documents/turo/auto.py", line 32, in setup_browser
driver = webdriver.Chrome(options=options)
File "/home/ubuntu/Documents/turo/venv/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py", line 76, in __init__
RemoteWebDriver.__init__(
File "/home/ubuntu/Documents/turo/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "/home/ubuntu/Documents/turo/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/ubuntu/Documents/turo/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/home/ubuntu/Documents/turo/venv/lib/python3.8/site-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.
(chrome not reachable)
(The process started from chrome location /snap/chromium/2198/usr/lib/chromium-browser/chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)