selenium模擬淘寶登陸,過所有驗證


淘寶模擬登陸實現

由於淘寶使用了滑動驗證碼,需要進行模糊手動滑動,因此考慮使用selenium+chromedriver進行模擬登陸。
淘寶的登陸網址:https://login.taobao.com/member/login.jhtml

項目運行准備:

安裝python3開發環境,官網自行下載,博主版本為3.7
安裝chrome游覽器,下載地址:https://www.google.cn/chrome/
安裝selenium模塊,在終端中輸入pip3 install selenium
安裝chromedriver驅動,下載地址:http://npm.taobao.org/mirrors/chromedriver/
注意:

  • 在下載chromedriver驅動的時候,需要與chrome游覽器的版本盡量保持一致,查看chrome游覽器版本方法,在谷歌游覽器訪問:chrome://settings/help這個網址

項目需要使用的所有模塊

import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

初始化selenium並加載chromedriver驅動

class TB(object):
    def __init__(self, *args, **kwargs):
        """
        初始化游覽器版本
        """
        option = webdriver.ChromeOptions()
        self.webdriver_obj = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=option)
        # 跳過阿里滑動驗證碼對selenium的校驗
        self.webdriver_obj.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
            "source": '''
            Object.defineProperty(navigator, 'webdriver', {
              get: () => undefined
            })
          '''
        })

封裝隱式等待進行xpath定位

由於selenium隱式等待進行xpath定位找不到會報錯,項目中多次使用這一段代碼,因此對這一部分代碼進行封裝,封裝之后的代碼如下:

def xpath(self, xpath_bas):
      try:
          element = WebDriverWait(self.webdriver_obj, 10).until(
              EC.presence_of_element_located((By.XPATH, xpath_bas))
          )
      except:
          element = []
      finally:
          return element

進行登錄標簽的定位,輸入賬號,密碼操作

    def login(self,username='', password=''):
        url = 'https://login.taobao.com/member/login.jhtml'
        self.webdriver_obj.get(url)
        self.xpath('//*[@id="fm-login-id"]').send_keys(username)  # 定位賬號輸入框,並輸入賬號
        self.xpath('//*[@id="fm-login-password"]').send_keys(password)  # 定位密碼輸入框,並輸入密碼
        self.xpath('//*[@id="login-form"]/div[4]/button').click()  # 第一次點擊登錄按鈕
        time.sleep(5)  
        check_yzm_span = self.xpath('//*[@id="nc_1_n1z"]')  # 延時定位滑動驗證碼標簽,避免點擊登錄按鈕之后,出現滑動驗證碼,不需要驗證的情況
        if bool(check_yzm_span):
            print('登錄發現滑動驗證碼')
            self.check_yzm(check_yzm_span)  # 處理滑動驗證碼
        else:
            print('登錄未發現滑動驗證碼')
        try:
            frame = self.xpath('//*[@id="content"]/div/div[1]/iframe')  # 定位iframe標簽,由於iframe沒有id,name這種唯一的屬性,因此只能通過先定位,在切換實現
            self.webdriver_obj.switch_to.frame(frame)  # 由於手機驗證頁面出現了頁面的嵌套,因此需要進行頁面跳轉到iframe下
            check_phone_button = self.xpath('//*[@id="J_GetCode"]')
            print('登錄發現手機驗證碼')
            self.check_phone_yzm(check_phone_button)  # 處理手機登錄驗證碼
        except:
            print('登錄未發現手機驗證碼')
        finally:
            print('登陸成功')

處理滑動驗證碼

定位到滑塊的標簽

定位滑動槽的標簽

通過定位發現滑動槽的長度是300,滑塊的長度是40,因此需要滑動的距離大約是260.
因此可以通過ActionChains動作鏈實現對滑動的拖動操作,具體代碼如下:

    def check_yzm(self, check_yzm_span):
        """
        處理滑動驗證碼,沒有測試
        :param check_yzm_span:
        :return:
        """
        # 實例化一個動作鏈關聯游覽器
        action = ActionChains(self.webdriver_obj)
        # 使用鼠標動作鏈進行點擊並懸浮
        action.click_and_hold(check_yzm_span)
        # 滑動驗證碼
        action.move_by_offset(xoffset=258, yoffset=0).perform()
        time.sleep(1)
        # 再次點擊登錄按鈕
        self.xpath('//*[@id="login-form"]/div[4]/button').click()

處理手機驗證碼

在點擊登錄之后,淘寶會進行身份驗證,就是讓你輸入手機驗證碼,然后對驗證碼進行輸入后,才能讓你登錄成功。

發現,淘寶這個頁面出現了頁面的嵌套,並且iframe沒有id,name這樣唯一的屬性值,因此只能通過先對iframe標簽進行定位,然后通過switch_to進行頁面的切換。
切換頁面之后,剩下的思路就是點擊獲取驗證碼按鈕,輸入驗證碼,點擊確認按鈕。具體實現代碼如下:

    def check_phone_yzm(self, check_phone_button):
        check_phone_button.click()
        yzm = input('請輸入手機驗證碼:')
        self.xpath('//*[@id="J_Phone_Checkcode"]').send_keys(yzm)
        self.xpath('//*[@id="submitBtn"]').click()

最后登陸成功之后的截圖如下:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM