【Selenium_Web_混合定位】(美团)大众点评霸王餐免费抽取,网址爬虫+自动报名,自动化测试案例

#!/usr/bin/env/python3
# -*- coding:utf-8 -*-
'''
Author:leo
Date&Time:2019-09-10 and 12:32
FileName:
Description:大众点评自动报名...
'''
from selenium import webdriver
from time import sleep
from pprint import pprint
from selenium.webdriver.support.select import Select
driver = webdriver.Chrome(r"D:\TestFiles\Python3\chromedriver.exe")
driver.implicitly_wait(2)
driver.get(r"http://s.dianping.com/event/chengdu")
more = driver.find_element_by_xpath("//a[@class='load-more show']")
mores = more.get_attribute("class")
addList = ['春熙路']
addStr = "双人"
successList = []
failList = []

# 主窗口柄和标题
mainWindows = driver.current_window_handle
mainTitle = driver.title
print(f"\n---- 1.Welcome,欢迎进入霸王餐首页:{mainTitle} ----\n")
sleep(5)

# 加载更多
more = driver.find_element_by_css_selector(".load-more.show")
# print(more)
try:
    while True:
        if more.get_attribute("class") == r"load-more show":
            print(f"-- 点击加载更多成功,请稍等片刻!--")
            more.click()
            sleep(1)
        elif more.get_attribute("class") == r"load-more":
            print("\n---- 2.Success!霸王餐信息已全部加载完毕!开始循环获取商铺信息----\n")
            break
except:
    print("-- 点击加载更多失败,请检查!--")
count = 0
menus = driver.find_elements_by_css_selector(".activity-lists-wraper li")
for i in menus:
    url = i.find_element_by_css_selector("a")
    storeUrl = i.find_element_by_css_selector("a").get_attribute("href")
    storeName = i.find_element_by_css_selector("h4").text
    storeAddr = i.find_element_by_css_selector("div.addi>span.addr").text
    count +=1
    # if storeAddr in addList:
    #     print(f"{count:<3}.附近商家姓名:{storeName} || 地址:{storeAddr} || 链接:{storeUrl}")
    if addStr in storeName or r"饮品" in storeName:
        print(f"{count:<3}.商家姓名:{storeName} || 地址:{storeAddr} || 链接:{storeUrl}")
        url.click()

        # 跳转新窗口
        for window in driver.window_handles:
            driver.switch_to.window(window)
            if window != mainWindows:
                break
                #--
        sleep(1.5)
        button = driver.find_element_by_css_selector(".btn-txt.J_self_apply")
        # print(f"新标签页面按钮为:{button.text}")
        sleep(1.5)
        button.click()
        sleep(5)# 手动登录时间
        try:
            # abert = driver.find_element_by_css_selector(".pop-main")
            # print(f"新标签页面弹框为:{abert.text}")
            # abert.click()
            driver.find_element_by_css_selector("#J_pop_ok").click()
            sleep(1)
            abert1 = driver.find_element_by_css_selector(".pop-main .col.tit")
            print(f"报名成功页面弹框为:{abert1.text}\n")
            sleep(1)
            # 关闭弹窗
            driver.find_element_by_css_selector(".hd a.close").click()
            sleep(1.5)
            successList.append(f"{count}.{storeName}|{storeUrl}")
        except Exception as e:
            print(e)
            print(f"报名异常,请人工检查连接地址:{count}.{storeName}:{storeUrl}\n")
            failList.append(f"{count}.{storeName}|{storeUrl}")
        finally:
            # 关闭新标签
            driver.close()


        # 切回主窗口
        driver.switch_to.window(mainWindows)
        # print(f"{count}.{storeName}店铺报名成功,成功回到主窗口:{driver.title}")
print(f"报名成功店铺总数:{len(successList)}")
for sucess in  successList:
    print(f"\n---- 3.店铺详情:{sucess} ----\n")

print(f"报名成功店铺总数:{len(failList)}")
for fail in  failList:
    print(f"\n---- 4.店铺详情:{fail} ----\n")



----------------------执行结果:由于第一次执行已经报名成功了70店铺,重新跑一次,不能再次报名,所以提示异常------------------

---- 1.Welcome,欢迎进入霸王餐首页:成都本地同城活动-成都霸王餐-大众点评网 ----

-- 点击加载更多成功,请稍等片刻!--
-- 点击加载更多成功,请稍等片刻!--
-- 点击加载更多成功,请稍等片刻!--

---- 2.Success!霸王餐信息已全部加载完毕!开始循环获取商铺信息----

1  .商家姓名:【VIP专享】肉桂厨房CINNAMON精致双人餐免费抢 || 地址:宽窄巷子 || 链接:http://s.dianping.com/event/848277703
Message: no such element: Unable to locate element: {"method":"css selector","selector":"#J_pop_ok"}
  (Session info: chrome=75.0.3770.100)

报名异常,请人工检查连接地址:1.【VIP专享】肉桂厨房CINNAMON精致双人餐免费抢:http://s.dianping.com/event/848277703

2  .商家姓名:【VIP专享】李八哥成都小盘菜火锅超值双人套餐免费抢 || 地址:高新区 || 链接:http://s.dianping.com/event/1714192068
Message: no such element: Unable to locate element: {"method":"css selector","selector":"#J_pop_ok"}
  (Session info: chrome=75.0.3770.100)

报名异常,请人工检查连接地址:2.【VIP专享】李八哥成都小盘菜火锅超值双人套餐免费抢:http://s.dianping.com/event/1714192068

3  .商家姓名:丸摩堂夏季福利招牌饮品免费抢 || 地址:多商圈 || 链接:http://s.dianping.com/event/320629907
Message: no such element: Unable to locate element: {"method":"css selector","selector":".pop-main .col.tit"}
  (Session info: chrome=75.0.3770.100)

报名异常,请人工检查连接地址:3.丸摩堂夏季福利招牌饮品免费抢:http://s.dianping.com/event/320629907

11 .商家姓名:书亦烧仙草双人饮品套餐免费抢 || 地址:多商圈 || 链接:http://s.dianping.com/event/319838408
Message: no such element: Unable to locate element: {"method":"css selector","selector":".pop-main .col.tit"}
  (Session info: chrome=75.0.3770.100)

报名异常,请人工检查连接地址:11.书亦烧仙草双人饮品套餐免费抢:http://s.dianping.com/event/319838408

15 .商家姓名:【VIP专享】堂前燕价值150元双人餐免费抢 || 地址:武侯祠 || 链接:http://s.dianping.com/event/234374367
Message: no such element: Unable to locate element: {"method":"css selector","selector":"#J_pop_ok"}
  (Session info: chrome=75.0.3770.100)

报名异常,请人工检查连接地址:15.【VIP专享】堂前燕价值150元双人餐免费抢:http://s.dianping.com/event/234374367

16 .商家姓名:小妖签串串香超值双人餐免费抢 || 地址:北门大桥 || 链接:http://s.dianping.com/event/618443768
Message: no such element: Unable to locate element: {"method":"css selector","selector":"#J_pop_ok"}
  (Session info: chrome=75.0.3770.100)

报名异常,请人工检查连接地址:16.小妖签串串香超值双人餐免费抢:http://s.dianping.com/event/618443768

20 .商家姓名:【吃住玩全包】价值3644元汉唐·馨阁民宿双人三天两晚休闲游 || 地址:西山公园 || 链接:http://s.dianping.com/event/884865365
Message: no such element: Unable to locate element: {"method":"css selector","selector":"#J_pop_ok"}
  (Session info: chrome=75.0.3770.100)

报名异常,请人工检查连接地址:

20.【吃住玩全包】价值3644元汉唐·馨阁民宿双人三天两晚休闲游:http://s.dianping.com/event/884865365


报名异常店铺总数:7

---- 4.店铺详情:1.【VIP专享】肉桂厨房CINNAMON精致双人餐免费抢|http://s.dianping.com/event/848277703 ----


---- 4.店铺详情:2.【VIP专享】李八哥成都小盘菜火锅超值双人套餐免费抢|http://s.dianping.com/event/1714192068 ----


---- 4.店铺详情:3.丸摩堂夏季福利招牌饮品免费抢|http://s.dianping.com/event/320629907 ----


---- 4.店铺详情:11.书亦烧仙草双人饮品套餐免费抢|http://s.dianping.com/event/319838408 ----


---- 4.店铺详情:15.【VIP专享】堂前燕价值150元双人餐免费抢|http://s.dianping.com/event/234374367 ----


---- 4.店铺详情:16.小妖签串串香超值双人餐免费抢|http://s.dianping.com/event/618443768 ----


---- 4.店铺详情:20.【吃住玩全包】价值3644元汉唐·馨阁民宿双人三天两晚休闲游|http://s.dianping.com/event/884865365 ----


Process finished with exit code 0

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章