Automate Coupon Snatching with Selenium: A Hands‑On Python Guide

This article explains why flash‑sale coupons are hard to grab, reveals the hidden gray‑market tactics, and provides a complete Python Selenium script plus CSS selector tips to simulate and practice automated coupon grabbing for learning purposes.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Automate Coupon Snatching with Selenium: A Hands‑On Python Guide

During holidays, many e‑commerce platforms release flash‑sale coupons ranging from a few yuan to "zero‑price" offers, but users often fail to grab them because the competition is fierce.

Some coupons are simply marketing tricks, while others are snapped up by a gray‑market industry that specializes in rapid coupon acquisition.

To test whether speed alone can win coupons, the author wrote a Python Selenium tool that simulates the grabbing process. The tool is intended solely for learning and has no commercial use.

Python Selenium Coupon‑Snatching Script

from selenium import webdriver
import datetime
import time

options = webdriver.ChromeOptions()
options.add_argument('--log-level=3')
# Create browser object
driver = webdriver.Chrome(options=options)
driver.maximize_window()

url = "https://pro.jd.com/mall/active/u6gHEpQdnEZuJPf8ebCQqdJCs2V/index.html?jd_pop=c0123941-4a86-4dbd-8f86-1cd540cd261d&utm_source=chongzhi.jd.com&utm_medium=zssc&utm_campaign=t_0_&utm_term=c0123941-4a86-4dbd-8f86-1cd540cd261d-p_93455"
driver.get(url)
driver.implicitly_wait(10)
time.sleep(2)

# Click login button
driver.find_element_by_link_text("你好,请登录").click()
print("请在30秒内完成登录")
time.sleep(10)

btn_buy = "[data-cpid='6F3BD5C7006031740B25BCBCF11343FC_babel']"
btn_close = ".close-button"
buy_time = "2019-08-04 09:59:00"

a = 0
while True:
    print("领取还未开始")
    if datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') > buy_time:
        print("即将开始领取")
        time.sleep(0.2)
        if driver.find_element_by_css_selector(btn_buy):
            while True:
                driver.find_element_by_css_selector(btn_buy).click()
                time.sleep(0.2)
                try:
                    if driver.find_element_by_css_selector(btn_close):
                        time.sleep(0.2)
                        driver.find_element_by_css_selector(btn_close).click()
                    time.sleep(0.2)
                except:
                    print("抢券成功")
                    a = 1
                    break
            if a == 1:
                break
    time.sleep(0.5)

Selenium Element‑Locating Tips

Selenium supports multiple browsers and can simulate user actions. When locating elements, CSS selectors are preferred over XPath because they are faster.

CSS selectors can be categorized into four types: id, class, attribute, and path. Below are examples for each.

Id Selector

element_input = driver.find_element_by_css_selector("#kw").send_keys('123')

Class Selector

element_input = driver.find_element_by_css_selector(".s_ipt").send_keys('123')

Attribute Selector

element_input = driver.find_element_by_css_selector("[name='wd']").send_keys('123')

Path Selector

element_input = driver.find_element_by_css_selector("form>span>input").send_keys('123')

Combined Selector Example

element_input = driver.find_element_by_css_selector("form>span.bg.s_ipt_wr.quickdelete-wrap>[name='wd']").send_keys('123')

Note: When a class value contains spaces, replace spaces with dots and prepend the tag name. Use the greater‑than sign ">" to denote hierarchy in path selectors.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythontestingSeleniumWeb Automationcss selectorCoupon Scraping
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.