python3.7+selenium模擬淘寶登錄功能的實(shí)現(xiàn)
在使用selenium去獲取淘寶商品信息時會遇到登錄界面
這個登錄界面處理的難度在于滑動驗(yàn)證的實(shí)現(xiàn),有的人使用微博登錄,避免了滑動驗(yàn)證,那可不可以使用密碼登錄呢?答案是可以的
實(shí)現(xiàn)思路
首先導(dǎo)入需要的庫
from selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver import ActionChainsimport time
1. 定位登錄元素,點(diǎn)擊跳轉(zhuǎn)
代碼如下:
password_login = self.wait.until( EC.presence_of_element_located((By.XPATH,'//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,'//div[@class=’input-plain-wrap input-wrap-loginid ’]//input[@class=’fm-text’]'))) input_user.send_keys(’用戶’) input_password = self.browser.find_element_by_xpath('//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)動路徑的實(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,所以滑動距離是260
5. 按照運(yùn)動路徑拖動滑塊
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,'//div[@class=’fm-btn’]//button[@type=’submit’]'))) button.click()
代碼整理
# encoding:utf-8from selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support import expected_conditions as ECfrom selenium.webdriver import ActionChainsimport timeclass 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,'//div[@class=’site-nav-sign’]//a[@class=’h’]'))) password_login.click() #登錄 input_user = self.wait.until( EC.presence_of_element_located((By.XPATH,'//div[@class=’input-plain-wrap input-wrap-loginid ’]//input[@class=’fm-text’]'))) input_user.send_keys(’用戶’) input_password = self.browser.find_element_by_xpath('//div[@class=’input-plain-wrap input-wrap-password’]//input[@class=’fm-text’]') input_password.send_keys(’密碼’) def _get_track(self): ’’’ 獲取運(yùn)動軌跡 :return: 運(yùn)動軌跡 ’’’ #滑動驗(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: 滑塊對象 ’’’ 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拖動滑塊 :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,'//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)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Python如何批量生成和調(diào)用變量2. ASP.NET MVC實(shí)現(xiàn)橫向展示購物車3. ASP.Net Core對USB攝像頭進(jìn)行截圖4. .net如何優(yōu)雅的使用EFCore實(shí)例詳解5. ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn)6. python 爬取京東指定商品評論并進(jìn)行情感分析7. python基礎(chǔ)之匿名函數(shù)詳解8. Python獲取B站粉絲數(shù)的示例代碼9. ajax動態(tài)加載json數(shù)據(jù)并詳細(xì)解析10. 通過CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動畫特效
