Description
Whenever there is a need to reschedule or repeat our groceries delivery on RedMart.com, there is a need to manually add back all the previous items one by one to create a new order. There is no option to repeat an order (unbelievable).
My wife is very frustrated with that, because in between clicks there is so much waiting time, and the website cannot be ctrl-clicked to open new tabs to do this efficiently. So I wrote below automation script for her. Instead of wasting time doing mindless work every week, now she can double-click this Python script and automate the process herself.
PS - we have since left Singapore and no longer use RedMart, thus below code would likely not work with updates to the website. But it should give you an idea of what's possible with rpa package and how you can approach a scenario.
import rpa as r
r.init()
# get URL of old groceries order to re-add all items to cart
r.url(r.ask('Paste the URL of your groceries order and login to RedMart'))
# set a maximum 5-minute timeout for user to login to RedMart
r.timeout(300)
# use exist() function with XPath to check if logged in
if not r.exist('(//*[@class="order-item"])[1]//*[@class="item-pic"]'):
r.dom('alert("Quitting as groceries order page is not detected after 5 min")')
# change back to default of 10 seconds to quit fast on error
r.timeout(10)
# get count of total items to place order for, using XPath identifier
total_items = r.count('//*[@class="order-item"]//*[@class="item-pic"]')
# to track total quantity of items to order
total_quantity = 0
# loop to add all the items and their quantity
for item in range(1, total_items + 1):
# get count of quantity to order for item, before clicking into item
item_quantity = int(r.read('(//*[@class="order-item"])['+ str(item) + ']//*[@class="item-quantity"]//*[@class="text"]'))
r.click('(//*[@class="order-item"])[' + str(item) + ']//*[@class="item-pic"]')
# first click ADD TO CART, then click + icon for subsequent quantity
if r.exist('ADD TO CART') and not r.present('Out of stock'):
# handle case where item is in cart, thus no ADD TO CART button
if r.present('next-icon-add'):
r.click('next-icon-add')
else:
r.click('ADD TO CART')
for additional_quantity in range(item_quantity - 1):
r.click('next-icon-add')
# wait to ensure adding of item has been registered before going back
r.wait(3)
total_quantity = total_quantity + item_quantity
# go back to the previous page with list of items
r.dom('history.back()')
# optional wait to slow down the pace of iteration in the automation
r.wait(3)
# show popup with summary of the quantity of items added before exiting
r.dom('alert("A total quantity of ' + str(total_quantity) + ' items has been added to cart")')
r.close()