def picking(method): # 打开购物车列表页面 browser.get("https://cart.taobao.com/cart.htm") time.sleep(3) # 是否全选购物车 if method == 0: while True: try: if browser.find_element_by_id("J_SelectAll1"): browser.find_element_by_id("J_SelectAll1").click() break except: print(f"找不到购买按钮") else: print(f"请手动勾选需要购买的商品") time.sleep(5)
复制代码
等待抢购时间,定时秒杀,这里我们定义一个buy函数
def buy(times): while True: now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') # 对比时间,时间到的话就点击结算 if now > times: # 点击结算按钮 while True: try: if browser.find_element_by_link_text("结 算"): browser.find_element_by_link_text("结 算").click() print(f"结算成功,准备提交订单") break except: pass # 点击提交订单按钮 while True: try: if browser.find_element_by_link_text('提交订单'): browser.find_element_by_link_text('提交订单').click() print(f"抢购成功,请尽快付款") except: print(f"再次尝试提交订单") time.sleep(0.01)