淘宝自动抢购, Webdriver浏览器常用的元素定位

#!/usr/bin/env python
'''
    作者:张铭达
    功能:淘宝秒杀购物
    版本:0.2
    日期2019-06-16
'''

from  selenium import webdriver
import time,datetime

driver = webdriver.Chrome()
driver.maximize_window()

username = '张铭达33333'
class TaoBao(object):
    def __init__(self):
        pass
    def login(self):
        driver.get('https://www.taobao.com')
        time.sleep(0.5)
        loginelement = driver.find_element_by_link_text("亲,请登录")
        if loginelement:
            loginelement.click()
        while True:
            try:
                login_status = driver.find_element_by_link_text('张铭达33333')
                print('已登录')
                return True
            except:
                print('等待扫码或密码登录...')
                time.sleep(1)

    def daojishi(self,buytime):
        '''
        :param buytime: 输入字符串时间
        :return: 返回当前距离购买时间还有多少天,小时,分钟,秒
        '''
        daojishi = datetime.datetime.strptime(buy_time, '%Y-%m-%d %H:%M:%S') - datetime.datetime.now()
        '''这个时间差为datetime.timedelta格式,返回值中days + seconds 为总计的时间,够一天的时间存在days里面,不足的存在seconds里面'''
        days = daojishi.days
        hour = daojishi.seconds // 3600 #不足一天,看有多少个小时。
        mins = (daojishi.seconds % 3600) // 60 #不足一小时的看有多少个分钟
        seconds = daojishi.seconds % 60 # 不足一分钟的时间看有多少秒
        return days,hour,mins,seconds #返回值便于更直观看剩余多少时间:时分秒
    def chose_commodity(self,commodity_list):
        # '''勾选指定商品'''
        if commodity_list:
            for commodity in commodity_list:
                commodity_path_id = '//label[@for="%s"]' % commodity
                try:
                    if driver.find_element_by_xpath(commodity_path_id):
                        driver.find_element_by_xpath(commodity_path_id).click()
                        print('勾选:', commodity_path_id, '成功')
                except:
                    print('勾选 %s 失败!' % commodity_path_id)
        else:
            # '''勾选购物车全部商品'''
            if driver.find_element_by_id("J_SelectAll1"):
                driver.find_element_by_id("J_SelectAll1").click()
                
    def buy_all(self,buy_time,commodity_list=None):
        # driver.get("https://cart.taobao.com/cart.htm")
        driver.find_element_by_id('mc-menu-hd').click() #点击购物车连接
        while True:
            #勾选商品
            self.chose_commodity(commodity_list)
            ''' 对比当前时间到的话就点击结算,字符串可以直接比较时间大小'''
            now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            if now > buy_time:
                print('正在购物......')
                try:
                    # 点击结算按钮
                    if driver.find_element_by_id("J_Go"):
                        driver.find_element_by_id("J_Go").click()
                        time.sleep(0.5)
                    #点击提交订单按钮
                    driver.find_element_by_link_text('提交订单').click()
                    print('订单已提交,请付款')
                except:
                    print('购物车没有商品或者已经提交。')
            else:
                days,hour,mins,seconds = self.daojishi(buy_time)
                print('当前{} 距离 {} 倒计时还有{}天 {}小时 {}分钟 {}秒'.format(now,buy_time,days, hour, mins, seconds))
                if days > 1 or hour > 1 or mins > 2:
                    time.sleep(60)
                    driver.refresh()
            time.sleep(1)


if __name__ == '__main__':
    taobao = TaoBao()
    if taobao.login():
        commodity_list = ['J_CheckBox_1338514994390',
                          'J_CheckBox_1337098032426',
                          'J_CheckBox_1328527465471']
        buy_time = '2019-06-18 23:59:59'
        taobao.buy_all(buy_time,commodity_list)
        # taobao.daojishi(buy_time)
go_shoping0.2

 

#!/usr/bin/env python
'''
    作者:张铭达
    功能:淘宝秒杀购物
    版本:0.2
    日期2019-06-16
'''

from  selenium import webdriver
import time,datetime

driver = webdriver.Chrome()
driver.maximize_window()

username = '张铭达33333'
class TaoBao(object):
    def __init__(self):
        pass
    def login(self):
        driver.get('https://www.taobao.com')
        time.sleep(0.5)
        loginelement = driver.find_element_by_link_text("亲,请登录")
        if loginelement:
            loginelement.click()
        while True:
            try:
                login_status = driver.find_element_by_link_text('张铭达33333')
                print('已登录')
                return True
            except:
                print('等待扫码或密码登录...')
                time.sleep(1)

    def daojishi(self,buytime):
        '''
        :param buytime: 输入字符串时间
        :return: 返回当前距离购买时间还有多少天,小时,分钟,秒
        '''
        daojishi = datetime.datetime.strptime(buy_time, '%Y-%m-%d %H:%M:%S') - datetime.datetime.now()
        '''这个时间差为datetime.timedelta格式,返回值中days + seconds 为总计的时间,够一天的时间存在days里面,不足的存在seconds里面'''
        days = daojishi.days
        hour = daojishi.seconds // 3600 #不足一天,看有多少个小时。
        mins = (daojishi.seconds % 3600) // 60 #不足一小时的看有多少个分钟
        seconds = daojishi.seconds % 60 # 不足一分钟的时间看有多少秒
        return days,hour,mins,seconds #返回值便于更直观看剩余多少时间:时分秒
    def chose_commodity(self,commodity_list):
        # '''勾选指定商品'''
        if commodity_list:
            for commodity in commodity_list:
                commodity_path_id = '//label[@for="%s"]' % commodity
                try:
                    if driver.find_element_by_xpath(commodity_path_id):
                        driver.find_element_by_xpath(commodity_path_id).click()
                        print('勾选:', commodity_path_id, '成功')
                except:
                    print('勾选 %s 失败!' % commodity_path_id)
        else:
            # '''勾选购物车全部商品'''
            if driver.find_element_by_id("J_SelectAll1"):
                driver.find_element_by_id("J_SelectAll1").click()

    def buy_all(self,buy_time,commodity_list=None):
        # driver.get("https://cart.taobao.com/cart.htm")
        time.sleep(1)
        driver.find_element_by_partial_link_text('购物车').click()#点击购物车连接
        # driver.find_element_by_id('mc-menu-hd').click() #点击购物车连接
        time.sleep(0.5)
        while True:
            #勾选商品
            self.chose_commodity(commodity_list)
            ''' 对比当前时间到的话就点击结算,字符串可以直接比较时间大小'''
            now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            if now > buy_time:
                print('正在购物......')
                try:
                    # 点击结算按钮
                    if driver.find_element_by_id("J_Go"):
                        driver.find_element_by_id("J_Go").click()
                        time.sleep(0.5)
                    #点击提交订单按钮
                    driver.find_element_by_link_text('提交订单').click()
                    print('订单已提交,请付款')
                except:
                    print('购物车没有商品或者已经提交。')
            else:
                days,hour,mins,seconds = self.daojishi(buy_time)
                print('当前{} 距离 {} 倒计时还有{}天 {}小时 {}分钟 {}秒'.format(now,buy_time,days, hour, mins, seconds))
                if days > 1 or hour > 1 or mins > 2:
                    time.sleep(60)
                    driver.refresh()
            time.sleep(1)


if __name__ == '__main__':
    taobao = TaoBao()
    if taobao.login(): #如果登录成功就勾选购物车,并且下单
        #商品列表lable标签 浏览器提前用开发者模式获取
        commodity_list = ['J_CheckBox_1338514994390',
                          'J_CheckBox_1337098032426',
                          'J_CheckBox_1328527465471']
        buy_time = '2019-06-18 23:59:59'
        taobao.buy_all(buy_time,commodity_list)
        # taobao.daojishi(buy_time)
0.3版本

 

三. 安装三大浏览器驱动driver

     1.chromedriver 下载地址:https://code.google.com/p/chromedriver/downloads/list

     2.Firefox的驱动geckodriver 下载地址:https://github.com/mozilla/geckodriver/releases/

     3.IE的驱动IEdriver 下载地址:http://www.nuget.org/packages/Selenium.WebDriver.IEDriver/
---------------------
作者:huhu8812
来源:CSDN
原文:https://blog.csdn.net/azsx02/article/details/68947429
版权声明:本文为博主原创文章,转载请附上博文链接!

posted on 2019-06-17 16:36  zhangmingda  阅读(765)  评论(1编辑  收藏  举报

导航