在使用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. 定位登录元素,点击跳转
代码如下:
password_login = self.wait.until(
EC.presence_of_element_located((By.XPATH,"//div[@class='site-nav-sign']//a[@class='h']")))
password_login.click()
```
<p>这样就可以从首页跳转到登录页面</p>
<p><strong>2. 获取用户和密码输入框,并输入信息</strong></p>
```python
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('密码')
```
<p><strong>3. 获取滑块元素</strong></p>
```python
slider = self.wait.until(
EC.element_to_be_clickable(
(By.XPATH, '//div[@class="scale_text slidetounlock"]//span[@class="nc-lang-cnt"]')))
```
<p><strong>4. 滑块运动路径的实现</strong></p>
```python
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))
```
<p>这里的260是根据框的大小计算出来的</p>
<p style="text-align: center"><img alt="在这里插入图片描述" src="https://img.94e.cn/media/uploads/full/20200703/2020052610244568.jpg"></p>
<p>从图中我们可以看出来,框的大小是300*40,所以滑动距离是260</p>
<p><strong>5. 按照运动路径拖动滑块</strong></p>
```python
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()
```
<p><strong>6. 最后一步:获取登录按钮,点击登录</strong></p>
```python
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()
总结
到此这篇关于python3.7+selenium模拟登录淘宝的文章就介绍到这了,更多相关Python selenium模拟淘宝登陆内容请搜索python博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持python博客!
标签:selenium
Powered By python教程网 鲁ICP备18013710号
python博客 - 小白学python最友好的网站!