from selenium import webdriver
import datetime
import time
import sys
from selenium.webdriver.common.by import By
def login():
# 打开京东登录页,并进行扫码登录
browser.get("https://www.jd.com")
time.sleep(5)
if browser.find_element(by=By.CLASS_NAME, value="link-login"):
browser.find_element(by=By.CLASS_NAME, value="link-login").click()
print("======请在30秒内完成登录")
time.sleep(15)
browser.get("https://cart.jd.com")
time.sleep(3)
now = datetime.datetime.now()
print('======login success:', now.strftime('%Y-%m-%d %H:%M:%S'))
time.sleep(5)
def buy(times, choose):
# 点击购物车里全选按钮
if choose == 2:
print("======请手动勾选需要购买的商品")
while True:
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
# 对比时间,时间到的话就点击结算
if now > times:
print("========进入购买环节===============")
if choose == 1:
while True:
try:
print("enter loop=======")
check_all = browser.find_element(by=By.NAME, value="select-all")
print("========111111111111===============")
checked_value = check_all.get_attribute("clstag")[-1]
while checked_value == "0":
check_all.click()
print("checked_value="+checked_value)
checked_value = check_all.get_attribute("clstag")[-1]
if checked_value == "0":
continue
else:
break
except:
print("======找不到购买按钮")
'''
# 点击结算按钮
while True:
try:
if browser.find_element(by=By.CLASS_NAME, value="common-submit-btn"):
browser.browser.find_element(by=By.CLASS_NAME, value="common-submit-btn").click()
print("======结算成功")
break
except:
print("======找不到结算按钮")
pass
'''
while True:
try:
if browser.find_element(by=By.CLASS_NAME, value="btn-area"):
browser.find_element(by=By.CLASS_NAME, value="btn-area").click()
print("======结算成功")
break
except:
print("======找不到结算按钮")
pass
while True:
try:
if browser.find_element(by=By.ID, value="order-submit"):
browser.find_element(by=By.ID, value="order-submit").click()
now1 = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f')
print("======抢购成功时间:%s" % now1)
except:
print("======再次尝试提交订单")
pass
time.sleep(0.01)
# 定位元素方式三种任何一个都可以使用过,实际使用自由组合。
# (1)id定位 driver.find_element_by_id("id")
# (2)name定位 driver.find_element_by_name("name")
# (3)class定位 driver.find_element_by_class_name("class_name")
# 抢购主函数
if __name__ == "__main__":
# times = input("请输入抢购时间,格式如(2018-09-06 11:20:00.000000):")
times = '2023-01-14 22:11:00.000000'
print("times=" + times)
# times=time.time()
start_time = '2023-01-14 22:11:00' # 抢购时间
timearry = time.strptime(start_time, "%Y-%m-%d %H:%M:%S")
# times = time.mktime(timearry)
# print("times="+times)
# print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'))
browser = webdriver.Chrome()
browser.maximize_window()
login()
# choose = int(input("到时间自动勾选购物车请输入“1”,否则输入“2”:"))
choose = 1
buy(times, choose)