Fully Automate Login and Banner Generation with Selenium, Requests and Click

Bob, Sun 20 August 2017, Modules

automation, Click, images, Pillow, PyBites Banner Generator, Requests, scraping, Selenium, selenium-requests, tools

In part 3 of the PyBites Banner Generator article series I show you how to automatically generate a banner with Requests and Selenium.

For both scripts I used Click to build the CLI interface.

The code for this tutorial is on Github.

Requests

Julian showed us some time ago How to Use Python Requests on a Page Behind a Login. It showed how we could POST to a webpage, pretty cool. I took this concept and wrote a quick interactive script to POST to the PyBites Banner Generator form and retrieve the generated banner:

Requests + Selenium == selenium-requests

Having achieved that I wanted to get a private PyBites banner. As detailed in part 2 to use PyBites logos we need to login (this is one of our 'live' tools).

Although you can perfectly use requests to login to your site as well, I wanted to try Selenium for this version. After all we use this module for this week's code challenge. To use requests in Selenium there is a nice package called selenium-requests that:

Extends Selenium WebDriver classes to include the request function from the Requests library, while doing all the needed cookie and request headers handling.

Awesome!

It was friendly to use, see the code here.

Some notes, also on how I used click:

Let's try it

This is Click's out-of-the-box niceness:

$ python private_banner.py  --help
Usage: private_banner.py [OPTIONS]

Options:
-n, --name TEXT
-l, --logo [news|challenge|special|article]
-i, --image TEXT
-t, --text TEXT
-b, --background / -nb, --no-background
-o, --outfile TEXT
-u, --username TEXT
-p, --password TEXT
--help                          Show this message and exit.

Note that I set username and password in my env:

$ env |egrep 'USERNAME|PASSWORD'
PASSWORD=...
USERNAME=...

Let's create a banner for this article:

$ python private_banner.py -n selenium-requests \
-l article -i https://pbs.twimg.com/media/C7bRQMoXUAEqTbI.jpg \
-t 'Fully Automate Login and Banner Generation with Selenium, Requests and Click' \
-b -o selenium-requests-banner.png

And voilĂ :

resulting banner from command line

Going to the GUI we see that the image persisted in the DB:

resulting banner from command line

Now imagine using this script to automatically generate 100 banners from a csv file, wouldn't that be cool?

Further reading

PyBites Banner Generator

Awesome Modules


Keep Calm and Code in Python!

-- Bob

PyBites Python Tips

Do you want to get 250+ concise and applicable Python tips in an ebook that will cost you less than 10 bucks (future updates included), check it out here.

Get our Python Tips Book

"The discussions are succinct yet thorough enough to give you a solid grasp of the particular problem. I just wish I would have had this book when I started learning Python." - Daniel H

"Bob and Julian are the masters at aggregating these small snippets of code that can really make certain aspects of coding easier." - Jesse B

"This is now my favourite first Python go-to reference." - Anthony L

"Do you ever go on one of those cooking websites for a recipe and have to scroll for what feels like an eternity to get to the ingredients and the 4 steps the recipe actually takes? This is the opposite of that." - Sergio S

Get the book