python3.7+selenium模擬淘寶登錄功能的實(shí)現(xiàn)
在使用selenium去獲取淘寶商品信息時(shí)會(huì)遇到登錄界面
這個(gè)登錄界面處理的難度在于滑動(dòng)驗(yàn)證的實(shí)現(xiàn),有的人使用微博登錄,避免了滑動(dòng)驗(yàn)證,那可不可以使用密碼登錄呢?答案是可以的
實(shí)現(xiàn)思路
首先導(dǎo)入需要的庫
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver import ActionChains import time
1. 定位登錄元素,點(diǎn)擊跳轉(zhuǎn)
代碼如下:
password_login = self.wait.until( EC.presence_of_element_located((By.XPATH,"http://div[@class='site-nav-sign']//a[@class='h']"))) password_login.click()
這樣就可以從首頁跳轉(zhuǎn)到登錄頁面
2. 獲取用戶和密碼輸入框,并輸入信息
input_user = self.wait.until( EC.presence_of_element_located((By.XPATH,"http://div[@class='input-plain-wrap input-wrap-loginid ']//input[@class='fm-text']"))) input_user.send_keys('用戶') input_password = self.browser.find_element_by_xpath("http://div[@class='input-plain-wrap input-wrap-password']//input[@class='fm-text']") input_password.send_keys('密碼')
3. 獲取滑塊元素
slider = self.wait.until( EC.element_to_be_clickable( (By.XPATH, '//div[@class="scale_text slidetounlock"]//span[@class="nc-lang-cnt"]')))
4. 滑塊運(yùn)動(dòng)路徑的實(shí)現(xiàn)
distance = 260 track = [] current = 0 # mid = distance*3/13 t = 1 v= 260 if current < distance: x = v*t current = current+x track.append(round(x))
這里的260是根據(jù)框的大小計(jì)算出來的
從圖中我們可以看出來,框的大小是300*40,所以滑動(dòng)距離是260
5. 按照運(yùn)動(dòng)路徑拖動(dòng)滑塊
ActionChains(self.browser).click_and_hold(slider).perform() for i in tracks: ActionChains(self.browser).move_by_offset(xoffset=i,yoffset=0).perform() time.sleep(1) ActionChains(self.browser).release().perform()
6. 最后一步:獲取登錄按鈕,點(diǎn)擊登錄
button = self.wait.until( EC.element_to_be_clickable((By.XPATH,"http://div[@class='fm-btn']//button[@type='submit']"))) button.click()
代碼整理
# encoding:utf-8 from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver import ActionChains import time class Taobao_login(object): def __init__(self): self.browser = webdriver.Chrome() self.browser.get('https://www.taobao.com') self.wait = WebDriverWait(self.browser,10) #登錄操作 def _put_info(self): #等待密碼登錄選項(xiàng)出現(xiàn)并跳轉(zhuǎn)登錄頁面 password_login = self.wait.until( EC.presence_of_element_located((By.XPATH,"http://div[@class='site-nav-sign']//a[@class='h']"))) password_login.click() #登錄 input_user = self.wait.until( EC.presence_of_element_located((By.XPATH,"http://div[@class='input-plain-wrap input-wrap-loginid ']//input[@class='fm-text']"))) input_user.send_keys('用戶') input_password = self.browser.find_element_by_xpath("http://div[@class='input-plain-wrap input-wrap-password']//input[@class='fm-text']") input_password.send_keys('密碼') def _get_track(self): ''' 獲取運(yùn)動(dòng)軌跡 :return: 運(yùn)動(dòng)軌跡 ''' #滑動(dòng)驗(yàn)證 distance = 260 track = [] current = 0 # mid = distance*3/13 t = 1 v= 260 if current < distance: x = v*t current = current+x track.append(round(x)) return track def _get_slider(self): ''' 獲取滑塊 :return: 滑塊對(duì)象 ''' slider = self.wait.until( EC.element_to_be_clickable( (By.XPATH, '//div[@class="scale_text slidetounlock"]//span[@class="nc-lang-cnt"]'))) return slider def _move_to_gap(self,slider,tracks): ''' 按照tracks拖動(dòng)滑塊 :param spider: 滑塊 :param tracks: 軌跡 :return: ''' ActionChains(self.browser).click_and_hold(slider).perform() for i in tracks: ActionChains(self.browser).move_by_offset(xoffset=i,yoffset=0).perform() time.sleep(1) ActionChains(self.browser).release().perform() def _login(self): #點(diǎn)擊登錄 button = self.wait.until( EC.element_to_be_clickable((By.XPATH,"http://div[@class='fm-btn']//button[@type='submit']"))) button.click() time.sleep(1) def run(self): self._put_info() time.sleep(1) # tracks = self._get_track() # slider = self._get_slider() # self._move_to_gap(slider,tracks) # time.sleep(1) # self._login() if __name__ == '__main__': login = Taobao_login() login.run()
總結(jié)
到此這篇關(guān)于python3.7+selenium模擬登錄淘寶的文章就介紹到這了,更多相關(guān)Python selenium模擬淘寶登陸內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python利用模糊哈希實(shí)現(xiàn)對(duì)比文件相似度
對(duì)比兩個(gè)文件相似度,python中可通過difflib.SequenceMatcher/ssdeep/python_mmdt/tlsh實(shí)現(xiàn),<BR>在大量需要對(duì)比,且文件較大時(shí),需要更高的效率,可以考慮模糊哈希,本文就來和大家詳細(xì)聊聊2023-01-01Python字典中的值為列表或字典的構(gòu)造實(shí)例
今天小編就為大家分享一篇Python字典中的值為列表或字典的構(gòu)造實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12利用matplotlib+numpy繪制多種繪圖的方法實(shí)例
matplotlib是Python最著名的繪圖庫,本文給大家分享了利用matplotlib+numpy繪制多種繪圖的方法實(shí)例,其中包括填充圖、散點(diǎn)圖(scatter plots)、. 條形圖(bar plots)、等高線圖(contour plots)、 點(diǎn)陣圖和3D圖,需要的朋友可以參考借鑒,下面來一起看看吧。2017-05-05python數(shù)據(jù)結(jié)構(gòu)的排序算法
下面是是對(duì)python數(shù)據(jù)結(jié)構(gòu)的排序算法的一些講解及示意圖,感興趣的小伙伴一起來學(xué)習(xí)吧2021-08-08Python3標(biāo)準(zhǔn)庫之dbm UNIX鍵-值數(shù)據(jù)庫問題
dbm是面向DBM數(shù)據(jù)庫的一個(gè)前端,DBM數(shù)據(jù)庫使用簡單的字符串值作為鍵來訪問包含字符串的記錄。這篇文章主要介紹了Python3標(biāo)準(zhǔn)庫:dbm UNIX鍵-值數(shù)據(jù)庫的相關(guān)知識(shí),需要的朋友可以參考下2020-03-03Python的進(jìn)制轉(zhuǎn)換和ASCLL轉(zhuǎn)換你了解嗎
這篇文章主要為大家詳細(xì)介紹了Python的進(jìn)制轉(zhuǎn)換和ASCLL轉(zhuǎn)換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-02-02Python?Opencv中獲取卷積核的實(shí)現(xiàn)代碼
這篇文章主要介紹了Python?Opencv中獲取卷積核的實(shí)現(xiàn)代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07python實(shí)現(xiàn)畫五角星和螺旋線的示例
今天小編就為大家分享一篇python實(shí)現(xiàn)畫五角星和螺旋線的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01