from selenium import webdriver
import datetime
import time
def login():
# 打开淘宝首页,扫码登陆淘宝
driver.get("https://www.XXXXXX.com")
time.sleep(3)
if driver.find_element_by_xpath("//*[@id='J_SiteNavLogin']/div[1]/div[1]/a[1]"):
driver.find_element_by_xpath("//*[@id='J_SiteNavLogin']/div[1]/div[1]/a[1]").click()
print("请在15秒内完成扫码")
time.sleep(10)
# 打开购物车列表首页
driver.get("https://cart.taobao.com/cart.htm")
time.sleep(3)
# 全选购物车
while True:
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
if datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") >= times and driver.find_element_by_id("J_SelectAll1"):
driver.find_element_by_id("J_SelectAll1").click()
break
print("select success"+datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
def buy(times):
while True:
# 对比时间,时间到的话就点击结算
try:
if driver.find_element_by_id("J_Go"):
driver.find_element_by_id("J_Go").click()
driver.find_element_by_link_text('提交订单').click()
print('抢购成功,请尽快付款')
except:
print('请再次尝试提交订单')
time.sleep(0.1)
if __name__ == "__main__":
driver = webdriver.Chrome()
times = input("请输入抢购时间(例如格式:):{}".format(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
login()
buy(times)