相思资源网 Design By www.200059.com

在使用selenium去获取淘宝商品信息时会遇到登录界面

python3.7+selenium模拟淘宝登录功能的实现

这个登录界面处理的难度在于滑动验证的实现,有的人使用微博登录,避免了滑动验证,那可不可以使用密码登录呢?答案是可以的

实现思路

首先导入需要的库

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. 定位登录元素,点击跳转

python3.7+selenium模拟淘宝登录功能的实现

代码如下:

password_login = self.wait.until(
 EC.presence_of_element_located((By.XPATH,"//div[@class='site-nav-sign']//a[@class='h']")))
 password_login.click()

这样就可以从首页跳转到登录页面

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. 滑块运动路径的实现

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是根据框的大小计算出来的

python3.7+selenium模拟淘宝登录功能的实现

从图中我们可以看出来,框的大小是300*40,所以滑动距离是260

5. 按照运动路径拖动滑块

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. 最后一步:获取登录按钮,点击登录

button = self.wait.until(
 EC.element_to_be_clickable((By.XPATH,"//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):
 #等待密码登录选项出现并跳转登录页面
 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):
 '''
 获取运动轨迹
 :return: 运动轨迹
 '''
 #滑动验证
 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):
 #点击登录
 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()

总结

标签:
Python,selenium模拟淘宝登陆,pythonselenium登录

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“python3.7+selenium模拟淘宝登录功能的实现”

暂无python3.7+selenium模拟淘宝登录功能的实现的评论...