Search code examples
pythonpython-3.xmodulescreenshot

open a browser page and take screenshot every n hour in python 3


I want to open a web page then take a screenshot every 2 hours via python. here is my code to open a page at every 2 hour interval

import time
import webbrowser
total_breaks = 12
break_count = 0

while(break_count < total_breaks):
    time.sleep(7200)
    webbrowser.open("https://mail.google.com/mail/u/2")
    break_count = break_count + 1

i followed Take a screenshot of open website in python script but didn't get any success I am using python 3.5 . I got a module wxpython but it supportts only 2.x . So is there way to take a screen shot every 2 hours using python 3


Solution

  • Here's what you have to with Selenium to get started.

    1. Install Selenium using pip by pip install selenium in the command prompt.

    1. Make sure you have the python path in your environment variables.

    1. Run this script by changing the email and the password below.

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    import time
    browser = webdriver.Firefox()
    browser.implicitly_wait(30)
    browser.get('https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1#identifier')
    email = browser.find_element_by_xpath('//*[@id="Email"]')
    email.clear()
    email.send_keys("[email protected]")  # change email
    email.send_keys(Keys.RETURN)
    password = browser.find_element_by_xpath('//*[@id="Passwd"]')
    password.clear()
    password.send_keys("password")  # Change Password
    password.send_keys(Keys.RETURN)
    time.sleep(10)
    browser.save_screenshot('screen_shot.png')
    browser.close()
    
    1. And finally you can run schtask on the saved python file from the command prompt. schtask is the CRON equivalent on windows.

    schtasks /Create /SC MINUTE /MO  120 /TN screenshot /TR "PATH_TO_PYTHON_FILE\FILE.py"