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: 滑塊對象
'''
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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Pytorch中的圖像增廣transforms類和預(yù)處理方法
這篇文章主要介紹了Pytorch中的圖像增廣和預(yù)處理方法(transforms類),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
詳解Python并發(fā)編程之創(chuàng)建多線程的幾種方法
這篇文章主要介紹了詳解Python并發(fā)編程之創(chuàng)建多線程的幾種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
基于Python利用Pygame實(shí)現(xiàn)翻轉(zhuǎn)圖像
這篇文章主要介紹了基于Python利用Pygame實(shí)現(xiàn)翻轉(zhuǎn)圖像,我們將了解如何使用Pygame翻轉(zhuǎn)圖像,要翻轉(zhuǎn)圖像,我們需要使用pygame.transform.flip(Surface,?xbool,?ybool)?方法,該方法被調(diào)用來根據(jù)我們的需要在垂直方向或水平方向翻轉(zhuǎn)圖像,下面來看看具體的實(shí)現(xiàn)過程吧2022-02-02
python 將json數(shù)據(jù)提取轉(zhuǎn)化為txt的方法
今天小編就為大家分享一篇python 將json數(shù)據(jù)提取轉(zhuǎn)化為txt的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
Python實(shí)現(xiàn)掃描局域網(wǎng)活動(dòng)ip(掃描在線電腦)
這篇文章主要介紹了Python實(shí)現(xiàn)掃描局域網(wǎng)活動(dòng)ip(掃描在線電腦),本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-04-04
matplotlib 對坐標(biāo)的控制,加圖例注釋的操作
這篇文章主要介紹了matplotlib 對坐標(biāo)的控制,加圖例注釋的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
python使用scapy掃描內(nèi)網(wǎng)IP或端口的方法實(shí)現(xiàn)
Scapy是一個(gè)Python程序,使用戶能夠發(fā)送,嗅探和剖析并偽造網(wǎng)絡(luò)數(shù)據(jù)包,本文主要介紹了python使用scapy掃描內(nèi)網(wǎng)IP或端口的方法實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10

