Grocery Hacking: Automate Your Grocery Shopping Using the Robot Framework | by Geoff Cox | Mar, 2022

OK, however doesn’t Alexa already present a method to buy merchandise? Absolutely, this why Amazon created Alexa?

Truly, it does! Pretty just lately, the Alexa app began permitting you to Shop your list. You merely add gadgets to your Alexa checklist after which use the Alexa app to pick out from an inventory of Contemporary or Entire Meals merchandise:

Alexa’s Store Your Listing characteristic. Picture credit score: Creator

I’ve already thought of boycotting Amazon as they aren’t essentially the most socially accountable firm. We already purchase an excessive amount of on Amazon and we have to help different grocery shops.

As a lot as a I really like the comfort, velocity, and costs on Amazon, she’s proper! So, for now, we’ll stick with purchasing at Safeway. Certain, Safeway (a subsidiary of Albertsons) is without doubt one of the 5 largest grocery chains within the US, however a minimum of it isn’t Amazon.

Safeway vs Amazon Contemporary pricing. Picture credit score: Creator

For those who haven’t heard of it, RPA is just a method of automating repetitive duties, which are usually finished by a human, with a software program robotic. These are sometimes duties finished in Graphical Person Interfaces (GUIs) like net browsers, MS Excel, e mail shoppers, and so forth…

Earlier than we get into how I constructed my bot, let’s focus on my objectives. As a software program engineer, I knew I might craft some code that fired off API requests to the Todoist and Safeway backends. Nevertheless, this may sidestep my need to play with the Robotic Framework. As a substitute, I opted to make use of the SeleniumLibrary with Robotic Framework, which beneath the hood would permit me to construct a bot that makes use of Selenium to manage a browser in a method that’s similar to how I’d in any other case manually use a browser to do my grocery purchasing.

Within the subsequent few sections, I’m going to be strolling via excerpts of my bot code. Please be happy to obtain the total supply code at github.com/redgeoff/grocery-shopping-bot

Reusable key phrases

The Robotic Framework refers to your typical programming language operate as a key phrase. We’re going to outline some key phrases in order that we are able to summary among the tough edges that you just’ll encounter when working with an internet site in a browser. Particularly, web sites are beasts that load many components asynchronously, which signifies that components might not but be seen or enabled. Bots are inherently a lot quicker than people so in the event you aren’t cautious, it’s simple on your bot to error out from the UI not but being prepared or as a result of different race situations happen. In fact, you possibly can insert Sleep statements in your code, which might simulate the timing {that a} human would encounter, however that is an anti-pattern as a result of:

  1. Timing can range based mostly on the community, the backend servers, and even your compute energy.

Get the checklist from Todoist

The very first thing we would like the bot to do is log in to Todoist, choose our desired checklist, after which save the checklist to reminiscence. The Todoist web page that lists our gadgets, appears like this:

Todoist purchasing checklist. Picture credit score: Creator

Purchase it once more

After we get the gadgets from Todoist, the following step is so as to add corresponding gadgets to our Safeway cart. Now, we might simply go to the Safeway search web page and seek for the merchandise, however the search web page orders by Finest Match and doesn’t will let you order by essentially the most just lately bought gadgets.

Safeway’s search web page. Picture credit score: Creator
Kind by Continuously Bought. Picture credit score: Creator

Seek for gadgets not bought earlier than

In case you are like me, and most of your grocery purchasing consists of the identical 300 or so gadgets, the bot may have accomplished virtually your complete checklist simply by purchasing from the Purchase It Once more (just lately bought) web page. The remaining handful of things in your checklist will possible be these that you’ve got by no means bought earlier than, e.g. you are attempting a brand new recipe or your typical model is out of inventory.

  1. The formatting of the gadgets in your checklist might not match, e.g. nutrigrain vs nutri-grain
  2. The gadgets in your checklist have names that don’t match the title of the Safeway merchandise. As you may anticipate, Safeway associates hidden key phrases with every product. For instance, you could have pepper jack cheese in your checklist, however the Safeway product could also be known as Cheese Slices Pepper Jack

Mark Todolist gadgets accomplished

By now, our bot created a cart with each merchandise it might discover. The subsequent step is to mark these things as accomplished on our Todoist checklist.

Ship an e mail with the checklist of things added to cart

The very last thing that I wished my bot to do was to ship an e mail that lists all of the gadgets that have been added to the cart. The e-mail ought to checklist the title of the merchandise because it appeared on my Todoist checklist and because it seems in my Safeway purchasing cart. I’m then capable of examine this e mail shortly to see if something appears misplaced. I’ve usually discovered that a minimum of a pair gadgets look funky.

Looking for “banana”, finds a cabbage. Picture credit score: Creator
Instance grocery purchasing bot e mail. Picture credit score: Creator

Right here’s a fast video that demos the way to run the bot:

Grocery purchasing bot demo. Video credit score: Creator
Output listing incorporates Robotic Framework logs. Picture credit score: Creator
Robotic Framework logs. Picture credit score: Creator

In case you are a Visible Studio (VS) Code consumer, I discovered that the Robot Framework Intellisense extension could be very useful.

Examine HTML components in Firefox. Picture credit score: Creator
Get WebElements    xpath=//li[contains(@class,'task_list_item')]
Take a look at XPath queries in Firefox. Picture credit score: Creator

What if you wish to run the bot with no UI (headless) and within the background? Straightforward, simply observe the instructions at Run Locally with Docker.

This bot is only a Minimal Viable Product (MVP). Listed here are some issues that I feel could possibly be added to make it much more helpful:

  1. Assuming {that a} human will carry out minor tweaks to the checklist after the bot has run, it needs to be potential for the bot to study from these tweaks. In different phrases, if the bot determines {that a} Yellow Onion was bought, when the unique merchandise on the checklist was onion, it might add onion => yellow onion to the Replacements checklist in your behalf.
  2. Key phrase enlargement could possibly be used so {that a} single phrase can characterize a basket of things. For instance, your loved ones might eat a whole lot of fruit and need the fruit key phrase to incorporate strawberries, raspberries, and blueberries. This characteristic may be good for ordering all of the groceries for a dish. Maybe, that is carried out as a part of the Replacements checklist, i.e. lasagne => ricotta, beef, eggs, ...
  3. Help for merchandise counts. For instance, 7 bananas would add 7 bananas to the purchasing cart. For now, the bot provides only one banana to the cart and a human has to extend the rely manually after the bot has completed executing.
  4. On Safeway’s product pages there’s typically an choice to clip a coupon for a particular product, i.e. it can save you some cash on the acquisition. The bot ought to do that for you.
  5. As proven above, Alexa’s store your checklist characteristic gives a UI that permits you to choose from an inventory of the highest search outcomes that match the merchandise in your purchasing checklist. There could possibly be worth in displaying such an interface after the bot has run to be able to make any wanted tweaks shortly.
  6. The bot might analyze your buying habits and make suggestions to purchase sure gadgets in bulk once you eat gadgets quick sufficient and when buying in bulk would lead to saving cash.
  7. Use machine studying to recommend merchandise which have by no means been shopped for (new merchandise) based mostly in your current preferences for model, value, taste, and dimension.
  8. Help for different grocery shops. The prevailing bot already helps any sensible speaker that integrates with Todoist, but it surely solely helps the grocery retailer, Safeway.

Most likely not. As a lot enjoyable because it was taking part in with the Robotic Framework, the usage of the framework, via the browser, led to a reasonably brittle resolution. If Todoist or Safeway determine to vary their web sites, the bot will more than likely break.

  1. Make it a lot quicker as navigating the GUI is SLOW.
  2. Cut back the quantity of assets that the bot consumes as making a RESTful API name takes lots much less assets than working a browser
  3. Cut back the chance for race situations which are encountered within the browser when a bot navigates the GUI quicker than a human
  4. Keep away from the complexity of working with popups, which can be added at any time to the GUI and should have little to do with the bot’s main goal

I’ve been utilizing the bot now for a number of weeks. I’ve discovered that it precisely builds a cart for about 90% of the gadgets on my checklist. I then go in and make some minor tweaks to my purchasing cart by way of the Safeway cellular app, however general, I’ve minimize down the weekly time I spend ordering groceries from about half-hour to five minutes. So, yeah, it really works fairly nicely for me!

Loved this text? If this bot have been was an easy-to-use service and supported your most popular grocery retailer and sensible speaker, would you pay for it? Please contemplate taking 3 minutes to answer these 3 quick questions

Here’s a checklist of useful assets talked about on this article:

  1. github.com/redgeoff/grocery-shopping-bot
  2. Robot Framework Quick Start: UI Automation Tutorial
  3. github.com/robotframework/robotframework

More Posts