Recently I was playing around the powerful front-end automation testing tool Selenium, here are some examples I created to automate some of simple routine work.
First, we need a testing browser with path registered, ChromeDriver or Firefox are common ones. Typically put the executable chromedriver in /usr/local/bin/chromedriver (or chromedriver.exe in C:/Users/%USERNAME/AppData/Local/Google/Chrome/Application/), don’t forget to register this path or specify when using it.
Second, write a Python script. Selenium can simulate user browsing actions. Here I provide three templates for sample usage. Including login with username and password, get content url, download all urls. By digging patterns on the page, one can write scripts to make Selenium automate a sequence of actions on webpages and find useful information.
Template 1:
Template 2:
Template 3:
Bonus: scheduler
The last job is putting the script as a routine job. I use cron in Linux: in the shell, initiate the cron job list:
crontab -e, add this new job occurring every 24 hours:
* */23 * * * ~/routineJobs/script.py
(meaning of *: second, hour, day of month, month, day of week)
Don’t forget to check the cron job status or log the job results in the python script:
/etc/init.d/cron status.