chromeDriver下载地址:http://chromedriver.storage.googleapis.com/index.html
下载相应的版本,将下载的chrome驱动程序chromedriver.exe复制到chrome浏览器的安装目录下,也就是appication目录下
(此步可能非必须)同时将此路径\chromedriver.exe增加到环境变量path中(我的电脑--》右键——》属性——》高级系统设置——》环境变量——》系统变量——》path)中
'''
自动登录功能
'''
import os
from selenium import webdriver
import datetime
import time
chromedriver = "C:/Program Files (x86)/Google/Chrome/Application/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
def login(uname, pwd):
driver.get("https://www.taobao.com")
print('已打开淘宝网')
if driver.find_element_by_link_text("亲,请登录"):
driver.find_element_by_link_text("亲,请登录").click();
time.sleep(1)
if driver.find_element_by_link_text("密码登录"):
driver.find_element_by_link_text("密码登录").click();
time.sleep(1)
if driver.find_element_by_name("TPL_username"):
driver.find_element_by_name("TPL_username").send_keys(uname);
time.sleep(1)
if driver.find_element_by_name("TPL_password"):
driver.find_element_by_name("TPL_password").send_keys(pwd);
time.sleep(2)
if driver.find_element_by_id("J_SubmitStatic"):
driver.find_element_by_id("J_SubmitStatic").click();
time.sleep(1)
driver.get("https://cart.taobao.com/cart.htm")
if driver.find_element_by_id("J_SelectAll1"):
driver.find_element_by_id("J_SelectAll1").click()
time.sleep(3)
if driver.find_element_by_link_text("结 算"):
driver.find_element_by_link_text("结 算").click();
now = datetime.datetime.now()
print('login success:', now.strftime('%Y-%m-%d %H:%M:%S'))
def buy_on_time(buytime):
while True:
now = datetime.datetime.now()
if now.strftime('%Y-%m-%d %H:%M:%S') == buytime:
while True:
try:
driver.find_element_by_link_text('提交订单').click()
except:
time.sleep(1)
time.sleep(0.1)
#中文账号的时候要给它编码一下,不然会出错
#login("中文账号".decode('utf-8'),'密码')
#login("英文账号",'密码')
#buy_on_time('2019-06-09 20:00:01')
这个版本每次运行时自动登录淘宝账号,但是!!!淘宝并不允许受控制的浏览器进行登录操作,所以一直登录不上
手动扫码登录,然后交给程序秒杀
运行过程:先点击结算,等到秒杀时间刷新网页提交购物车中的商品到订单
'''
手动登录
'''
import os
from selenium import webdriver
import datetime
import time
chromedriver = "C:/Program Files (x86)/Google/Chrome/Application/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
def login(uname, pwd):
driver.get("https://www.taobao.com")
print('已打开淘宝网')
#设置等待时间,用于手动登录
time.sleep(60)
driver.get("https://cart.taobao.com/cart.htm")
if driver.find_element_by_id("J_SelectAll1"):
driver.find_element_by_id("J_SelectAll1").click()
time.sleep(3)
if driver.find_element_by_link_text("结 算"):
driver.find_element_by_link_text("结 算").click();
now = datetime.datetime.now()
print('login success:', now.strftime('%Y-%m-%d %H:%M:%S'))
def buy_on_time(buytime):
while True:
now = datetime.datetime.now()
if now.strftime('%Y-%m-%d %H:%M:%S') == buytime:
while True:
try:
driver.refresh() #刷新页面
driver.find_element_by_link_text('提交订单').click()
print('尝试提交订单')
except:
time.sleep(1)
time.sleep(0.1)
#中文账号的时候要给它编码一下,不然会出错
#login("中文账号".decode('utf-8'),'密码')
#login("英文账号",'密码')
功能:
1.秒杀任意商品,包括不能加入购物车的商品
2.加入隐形等待和显性等待,不必等网页全部加载完毕,只要页面想要的元素加载完成了就执行下一步。可以实现监测快速点击。
代码链接:https://download.csdn.net/download/weixin_43002202/11976967
关于等待方式详解参考链接https://www.jb51.net/article/92672.htm