标签:support 京东 事先 elf src 测试 %s == 按钮
环境:python3.7
浏览器:火狐
在python scripts文件夹下边放入相应版本的geckodriver(或手动把geckodriver加入环境变量)
cmd页面:
pip install selenium
pip install datetime
事先需要把商品放入购物车,电脑时间和北京时间一致
(建议至少提前10分钟操作)
from selenium import webdriver
import time
import datetime
import json
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
class jd_buy_plus():
def __init__(self, buy_time):
self.buy_time = buy_time
def driver(self):
option = webdriver.FirefoxOptions()
option.add_argument('-headless')
self.webdriver = webdriver.Firefox(firefox_options=option) # 使用无头模式提高效率,测试时可取消无头模式,括号为空即可
self.webdriver.maximize_window()
self.webdriver.implicitly_wait(5) # 隐式等待
self.webdriver.get("https://cart.jd.com/cart.action") # 此为购物车网站
def cookie(self): # 此方法作用为读取cookie_file.txt文件,并加载cookie
file = open(r"./cookie_file.txt")
cookie = file.read()
cookie = json.loads(cookie)
for i in cookie:
self.webdriver.add_cookie(i)
self.webdriver.refresh()
def buy_on_time(self): # 主要实现抢购功能方法
while True:
now = datetime.datetime.now()
title = self.webdriver.title
if now.strftime('%Y-%m-%d %H:%M:%S') == self.buy_time: # 判断是否到抢购时间
for i in range(1, 21): # 每隔0.2秒抢购一次,尝试抢购20次
print(now.strftime('%Y-%m-%d %H:%M:%S'))
print("第%d次抢购" % i)
self.webdriver.find_element(By.CSS_SELECTOR, '.jdcheckbox').click() # 此处点击的是在购物车页面选中商品的按钮
self.webdriver.find_element_by_link_text("去结算").click()
if (self.webdriver.title == title): # 如果检测到如果title发生改变,即点击成功,跳出循环
time.sleep(0.2)
else:
break
element = (By.CSS_SELECTOR, '.checkout-submit')
self.webdriver.execute_script('arguments[0].scrollIntoView();',
self.webdriver.find_element(*element)) # 拉滚动轴到元素顶端显示
self.webdriver.find_element(*element).click() # 创建订单成功,自行进行付款
print('purchase success')
quit()
time.sleep(0.5)
if __name__ == '__main__':
demo = jd_buy_plus("2020-03-01 12:34:00") # 填入开始抢购时间
demo.driver()
demo.cookie()
demo.buy_on_time()
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import json
driver = webdriver.Firefox()
driver.get("https://cart.jd.com/cart.action") # 此为购物车网站
driver.find_element(By.CSS_SELECTOR,'.login-btn').click() # 一般需要登录,此处点击的是去登录按钮
url = driver.current_url
while True: # 请在此期间进行扫码登录
if url == driver.current_url: # 检测页面跳转
time.sleep(0.5)
continue
else:
break
cookie = driver.get_cookies()
cookie = json.dumps(cookie)
file = open(r'./cookie_file.txt','w')
file.write(cookie)
driver.quit()
标签:support 京东 事先 elf src 测试 %s == 按钮
原文地址:https://www.cnblogs.com/yingyingdeyueer/p/12389279.html