-
selenium 介绍
- Selenium是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera,Edge等。这个工具的主要功能包括:测试与浏览器的兼容性——测试应用程序看是否能够很好得工作在不同浏览器和操作系统之上。测试系统功能——创建回归测试检验软件功能和用户需求。支持自动录制动作和自动生成.Net、Java、Perl等不同语言的测试脚本。
- 测试环境: python 3.6
chronm 119.0.6045.124
![]()
3. pip install selenium== 4.0.0a1
pip install urllib3==1.26.0
注意:先卸载后再更换版本 pip uninstall xxxxx
pip install xxxx -i + 镜像源
4. chronm 驱动下载地址:Chrome for Testing availability
https://googlechromelabs.github.io/chrome-for-testing/#stable ①是版本 ②是浏览器下载地址 ③是驱动器下载地址
解压后将下面文件放置在python 目录文件夹中,可以降低代码报错(路径问题导致的)
完成上述步骤后,可以完成简单的自动登录网页、窗口大小设置、界面图片截取等等。
import time
from selenium import webdriver
driver = webdriver.Chrome() # 创建驱动对象
driver.get('https://www.baidu.com') # 打开百度网页
driver.set_window_size(300,300) # 设置窗口尺寸
# driver.maximize_window() # 窗口最大化
time.sleep(3)
-
超级鹰 介绍
简单来讲,这是一个获取数据的平台(付费)。 用户登录-超级鹰验证码识别代答题平台https://www.chaojiying.com/user/login/?url=/user/mysoft/
根据程序语言下载对应的程序包
-
案例
一、滑块验证
import time
import requests
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from chaojiying_Python.chaojiying import Chaojiying_Client
class my_spider: # 自定义一个爬虫类
def __init__(self):
self.url = 'https://study.xiaoe-tech.com/t_l/learnLogin#/wx'
self.web = webdriver.Chrome()
self.web.maximize_window()
self.wait = WebDriverWait(self.web,50) # 对应的包 from selenium.webdriver.support.wait import WebDriverWait
def get_html(self):
self.web.get(self.url)
self.wait.until(expected_conditions.presence_of_element_located((By.CLASS_NAME,'wx-des')))
self.web.find_element(By.CLASS_NAME,'wx-des').click()
time.sleep(2)
self.wait.until(expected_conditions.presence_of_element_located((By.XPATH, '//div[@class="login-account-title"]//p[2]')))
self.web.find_element(By.XPATH, '//div[@class="login-account-title"]//p[2]').click()
time.sleep(2)
self.wait.until(expected_conditions.presence_of_element_located((By.CLASS_NAME,'ss-input__inner')))
self.web.find_element(By.CLASS_NAME,'ss-input__inner').send_keys('18318900727')
self.wait.until(expected_conditions.presence_of_element_located((By.XPATH,'//div[@class="inputBox"]/input')))
self.web.find_element(By.XPATH,'//div[@class="inputBox"]/input').send_keys('123')
time.sleep(2)
self.wait.until(expected_conditions.presence_of_element_located((By.CLASS_NAME,'login-btn')))
self.web.find_element(By.CLASS_NAME,'login-btn').click()
time.sleep(2)
# 滑块图是在内置页面中,所以必须先进入内置页面
self.web.switch_to.frame('tcaptcha_iframe')
# print(self.wait.until(expected_conditions.presence_of_element_located((By.CLASS_NAME,'body-wrap'))))
# self.web.find_element(By.XPATH,'//div[@class="tc-imgarea drag"]/div[2]/img').screenshot('小鹅通截图.png')
"""
# 将滑块图下载保存
self.wait.until(expected_conditions.presence_of_element_located((By.ID,'slideBlock')))
add = self.web.find_element(By.ID,'slideBlock').get_attribute('src')
print(add) # 图片地址
img = requests.get(add).content # 向地址发请求,获取图片数据二进制数据
with open('滑块图.png','wb') as f: # 将数据保存在文件 滑块图.png 中
f.write(img)
"""
# 将缺口图片下载保存
self.wait.until(expected_conditions.presence_of_element_located((By.XPATH,'//*[@id="cdn1"]')))
add = self.web.find_element(By.XPATH,'//*[@id="cdn1"]').get_attribute('src')
img_01 = requests.get(add).content
with open('缺口图.png','wb') as f:
f.write(img_01)
# 定位鼠标起始位置的标签
begin = self.web.find_element(By.XPATH,'//*[@id="tcaptcha_drag_thumb"]')
print(begin)
# 从超级鹰平台获取水平移动距离 x (滑块移动只针对水平距离,纵向位移为零)
self.get_num()
action = ActionChains(self.web) # 创建动作对象,目的是为了完成后续的 用鼠标拖动滑块验证
action.click_and_hold(begin) # 动作 1:在起始位置按住鼠标不放
action.move_by_offset(int(self.x)//2,0 ) # 动作 2:移动到指定坐标
action.move_by_offset(-50,0)
time.sleep(3) # 延时看效果
action.release() # 动作 3:松开鼠标
action.perform() # 完成以上3步动作
def get_num(self):
chaojiying = Chaojiying_Client('**********', '*********', '**********')
im = open('缺口图.png', 'rb').read()
ret = chaojiying.PostPic(im, 9101)
print(ret)
self.x = ret['pic_str'].split(',')[0]
if __name__ == '__main__':
my_spider().get_html()
time.sleep(3)
总结
案例一:- html页面中有嵌套页面,如滑块缺口图片就是。使用 web.switch_to.frame('tcaptcha_iframe')方法,进入嵌套页面。
- 动作链常用方法动作链:
ActionChains 方法列表:
click(on_element=None) -----------------------------------------——单击鼠标左键
click_and_hold(on_element=None) ---------------------------------——点击鼠标左键,不松开
context_click(on_element=None) ----------------------------------——点击鼠标右键
double_click(on_element=None) -----------------------------------——双击鼠标左键
drag_and_drop(source, target)-------------------------------------—拖拽到某个元素然后松开
drag_and_drop_by_offset(source, xoffset, yoffset) ---------------——拖拽到某个坐标然后松开
key_down(value, element=None) -----------------------------------——按下某个键盘上的键
key_up(value, element=None) -------------------------------------——松开某个键
move_by_offset(xoffset, yoffset) --------------------------------——鼠标从当前位置移动到某个坐标
move_to_element(to_element) -------------------------------------——鼠标移动到某个元素
move_to_element_with_offset(to_element, xoffset, yoffset) ------——移动到距某个元素(左上角坐标)多少距离的位置
perform() ------------------------------------------------------——执行链中的所有动作
release(on_element=None) —---------------------------------------—在某个元素位置松开鼠标左键
send_keys(*keys_to_send) ---------------------------------------——发送某个键到当前焦点的元素
send_keys_to_element(element, *keys_to_send) -------------------——发送某个键到指定元素- python 安装selenium的版本不合适会出现验证错误
- iframe : IFRAME是HTML标签,作用是文档中的文档,或者浮动的框架(FRAME)。
iframe元素会创建包含另外一个文档的内联框架(即行内框架)。
页面中存在iframe标签时,表示为一个内置页面,selenium 是不能直接找到这个内置页面中的标签对象
找内置页面中的标签对象方法: 驱动对象.switch_to.frame(' ') 参数一般是id 值