需求(步骤):
1. 打开网址 https://www.taobao.com/
2. 点击登录(此处练习通过扫码登录)
3. 点击进入购物车 https://cart.taobao.com/cart.htm
4. 全选商品
5. 点击结算
6. 点击提交订单 生成一个订单 代表着商品已经抢购了
from selenium import webdriver
import time,datetime
# 登录淘宝
def login():
# 进入主页
driver.get(https://www.taobao.com/)
# 通过点击购物车进入登录界面
driver.find_element_by_xpath(//*[@id="J_SiteNavLogin"]/div[1]/div[1]/a[1]).click()
# 选择登录界面
## 1.账户-密码-登录 (此处不采取这种方式,因为采用selenium登录,对方能够察觉到,设置了验证操作,登录进去较慢)
# driver.find_element_by_id(fm-login-id).send_keys(1212313)
# driver.find_element_by_id(fm-login-password).send_keys(fdfdfdf)
# driver.find_element_by_class_name(fm-button).click()
## 2.通过手机扫描二维码
time.sleep(3)
driver.get(https://cart.taobao.com/cart.htm)
now = datetime.datetime.now()
print(登录成功,当前时间为, now.strftime(%Y-%m-%d %H:%M:%S))
# 秒抢开始
def buy(time):
while True:
now = datetime.datetime.now().strftime(%Y-%m-%d %H:%M:%S.%f)
if now > time:
# 点击全选
while True:
try:
if driver.find_element_by_xpath(//*[@id="J_SelectAll1"]/div):
driver.find_element_by_xpath(//*[@id="J_SelectAll1"]/div).click()
break
except:
print(找不到全选按钮)
time.sleep(1)
# 提交订单
time.sleep(3)
while True:
try:
if driver.find_element_by_xpath(//*[@id="submitOrderPC_1"]/div/a[2]):
driver.find_element_by_xpath(//*[@id="submitOrderPC_1"]/div/a[2]).click()
break
except:
print(找不到结算按钮)
if __name__ == __main__:
time = input(请输入抢购的时间:)
driver = webdriver.Chrome()
login()
buy(time)