使用selenium模拟登陆,手机验证码

大众点评

from time import sleep
from selenium import webdriver
import random

def process_request(self,request,spider):
    
      phone = '电话号码'
      driver = webdriver.Chrome()
      driver.get('http://www.dianping.com/')
      #登陆
      sleep(random.uniform(1,3))
      elem = driver.find_element_by_xpath(r'//*[@div="top-nav"]/div/div[2]/span[2]/a[1]')
       elem.click()
       #切入网页框架
        sleep(random.unifrom(1,3))
        driver.switch_to.frame(driver.find_element_by_xpath(r'//*[@id="J_login_container"]/div/iframe'))
        # 切入 # 点击账号登录
        driver.find_element_by_xpath(r"/html/body/div/div[2]/div[5]/span").click()
        # print(driver.page_source)
        #  输入验证码
        sleep(random.uniform(1, 3))
        driver.find_element_by_xpath(r'//*[@id="mobile-number-textbox"]').send_keys(phone[:3])
        sleep(random.uniform(0, 2))
        driver.find_element_by_xpath(r'//*[@id="mobile-number-textbox"]').send_keys(phone[3:7])
        sleep(random.uniform(0, 2))
        driver.find_element_by_xpath(r'//*[@id="mobile-number-textbox"]').send_keys(phone[7:])
        # 点击获取验证码,等待输入
        sleep(random.uniform(1, 3))
        driver.find_element_by_xpath(r'//*[@id="send-number-button"]').click()
        key = input('请输入验证码:')
        driver.find_element_by_xpath(r'//*[@id="number-textbox"]').send_keys(key)
        # 点击登陆
        sleep(random.uniform(0, 1))
        driver.find_element_by_xpath(r'//*[@id="login-button-mobile"]').click()
        driver.switch_to.default_content()
        # 切出框架
        sleep(random.uniform(1,5))
        # 处理cookie
        print(request)
        cookie = driver.get_cookies()
        print(cookie)
        result = {}
        for each in cookie:
            result[each['name']] = each['value']
            return result

 

posted @ 2018-10-02 16:48  Caionk  阅读(3234)  评论(0编辑  收藏  举报