0

I have pulled a few things from other answers today (this for instance: open a browser page and take screenshot every n hour in python 3)

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Firefox()
browser.implicitly_wait(30)
browser.get('https://weather.com/en-AU/weather/today/l/ASXX0023:1:AS?Goto=Redirected')
time.sleep(10)
browser.save_screenshot('screen_shot.png')
browser.close()

this solution is working perfectly except I'm not sure how to execute in crontab.

i tried this after typing crontab -e then:

*/30 * * * * export DISPLAY=:0 /usr/local/bin/python3.9 /Users/path/script.py >> /path/screenshot.png

and it opens the firefox window, but the screenshot is Zero Bytes.. so it's close to what I want, but not quite there.

I tried a few other ways too - with script.sh - though I'm not to sure how to get that working either.

Also - it'd be good to add the time and date to the screenshot, so any help there is a blessing.

could you offer any guidance? Thanks!

ooo
  • 3
  • 1
  • The shell redirection `>> /path/screenshot.png` looks wrong but harmless. I'm guessing you are doing something even more bewildering in your real code. Probably redirect the script's output (and standard error) to a log file instead. – tripleee Jun 30 '21 at 07:05
  • The `DISPLAY=:0` looks wrong for a Mac, or are you really running X11? Again, it should be harmless _per se,_ but betrays a poor understanding of the pieces you have cobbled together. – tripleee Jun 30 '21 at 07:06
  • Yep - I'm a few weeks into python and this is my first attempt at cron - not even sure what X11 is, but DISPLAY=:0 was recommended on most other things, and actually got it working. The code I'm using is exactly the code above, unfortunately. – ooo Jun 30 '21 at 07:07
  • X11 is a GUI system common on Linux and Unix system, but Macs use their own and quite distinct framework, called Darwin. – tripleee Jun 30 '21 at 07:11
  • `/Users/name/code/screenshot.png` is the crontab path I'm using. there was no mention of Darwin on any previous sites - I'll look into it. – ooo Jun 30 '21 at 07:14
  • Probably the sites you have explored explain how to do this on Linux, which requires setting up an X11 server if you don't already have one, and give `cron` access to the currently open display. There are many reasons that's complicated, but if you are on a Mac and are certain to be logged into the GUI at all times, you don't need to worry about those complications. – tripleee Jun 30 '21 at 07:17
  • Does `/Users/name/code` exist and is it writable by the user whose `cron` job this is? Your `cron` job will write to a file in your home directory, though. Probably you are simply examining the wrong file. – tripleee Jun 30 '21 at 07:18

1 Answers1

0

The cron job will start in your home directory, and writes the screenshot to the current directory.

The cron job you have will also write the standard output from Python to a misleadingly named PNG file which will probably be empty. Probably this is the file you have been examining.

Probably restructure to switch to a different directory (or modify the Python script to read an output file name as its command-line argument) and write the script's output to a differently named file, also then including standard error. Furthermore, the DISPLAY variable does nothing useful on a Mac, so probably drop it.

*/30 * * * * cd code; /usr/local/bin/python3.9 /Users/path/script.py >> screenshot_$(date +\%F).log 2>&1

This also illustrates how to create a new log file for each separate date. The requirement to backslash the percent sign in crontab is a common trip-up.

If instead, or as well, you would like the Python script to generate a unique file name for each invocation, try something like

from datetime import datetime

... 
browser.save_screenshot(
    'screen_shot_' + datetime.now().strftime('%F_%T') + '.png')

This will generate a file like screen_shot_2021-06-30_10:26:26.png; maybe play around with the argument to strftime() if you would like a different date or time format.

tripleee
  • 175,061
  • 34
  • 275
  • 318