selenium提供模拟鼠标操作的功能,常见的鼠标操作有:单击、右键点击、双击、移动鼠标、拖动鼠标
selenium工具中全局鼠标操作语法如下:
1. 初始化ActionChains类(动作链条): actions = ActionChains(driver)
2. 找到要操作的元素: find_element
3. 调用鼠标操作方法: actions.move_to_element(element)
4. 执行鼠标操作方法: actions.perform()
具体用法: ActionChains(driver).click(element).perform()
image.png
# 鼠标点击“百度一下,id=“su””
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.baidu.com/")
driver.find_element(By.ID, 'kw').send_keys("selenium")
actions = ActionChains(driver)
element = driver.find_element(By.ID, 'su')
actions.click(element).perform()
time.sleep(3)
driver.close()
image.png
# 双击
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://sahitest.com/demo/clicks.htm")
actions = ActionChains(driver)
element = driver.find_element(By.CSS_SELECTOR, 'body > form > input[type=button]:nth-child(8)')
actions.double_click(element).perform()
actions.double_click(element).perform()
actions.double_click(element).perform()
time.sleep(3)
driver.close()
image.png
# 右键点击
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://sahitest.com/demo/clicks.htm")
actions = ActionChains(driver)
element = driver.find_element(By.XPATH, '/html/body/form/input[4]')
actions.context_click(element).perform()
time.sleep(3)
driver.close()
image.png
image.png
# 悬浮
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https:www.baidu.com")
actions = ActionChains(driver)
element = driver.find_element(By.CSS_SELECTOR, '#s-top-left > div > a')
actions.move_to_element(element).perform() # 悬浮
driver.find_element(By.XPATH, '//*[@id="s-top-more"]/div[3]/a').click()
time.sleep(3)
driver.close()
image.png
image.png
# 拖动
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://sahitest.com/demo/dragDropMooTools.htm")
actions = ActionChains(driver)
element_start = driver.find_element(By.ID, 'dragger')
element_end = driver.find_element(By.XPATH, '/html/body/div[5]')
actions.drag_and_drop(element_start, element_end).perform()
time.sleep(3)
driver.close()
image.png
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://www.iviewui.com/view-ui-plus/component/form/slider")
actions = ActionChains(driver)
# 双击
# element = driver.find_element(By.CSS_SELECTOR, 'body > form > input[type=button]:nth-child(8)')
# actions.double_click(element).perform()
# 右击
# element = driver.find_element(By.XPATH, '/html/body/form/input[4]')
# actions.context_click(element).perform()
# 悬浮
# element = driver.find_element(By.CSS_SELECTOR, '#s-top-left > div > a')
# actions.move_to_element(element).perform()
# driver.find_element(By.XPATH,'//*[@id="s-top-more"]/div[1]/a[3]').click()
# 拖动
# element_start = driver.find_element(By.ID, 'dragger')
# element_end = driver.find_element(By.XPATH, '/html/body/div[5]')
# actions.drag_and_drop(element_start, element_end).perform()
# 根据坐标点拖动
element = driver.find_element(By.XPATH, '//div[@class="ivu-slider-button"]')
actions.drag_and_drop_by_offset(element, 250, 0).perform()
time.sleep(3)
driver.close()
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://passport.ctrip.com/user/member/fastOrder")
element = driver.find_element(By.XPATH, '//*[@id="slider"]/div[1]/div[2]')
ActionChains(driver).drag_and_drop_by_offset(element, 300, 0).perform()
time.sleep(3)
driver.close()
import re
import requests
import time
from io import BytesIO
import cv2
import numpy as np
from PIL import Image
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
class CrackSlider():
# 通过浏览器截图,识别验证码中缺口位置,获取需要滑动距离,并破解滑动验证码
def __init__(self):
super(CrackSlider, self).__init__()
self.opts = webdriver.ChromeOptions()
self.opts.add_experimental_option('excludeSwitches', ['enable-logging'])
# self.driver = webdriver.Chrome(ChromeDriverManager().install(), options=self.opts)
self.driver = webdriver.Chrome(options=self.opts)
self.driver.maximize_window()
self.url = 'https://icas.jnu.edu.cn/cas/login'
self.wait = WebDriverWait(self.driver, 10)
def get_pic(self):
self.driver.get(self.url)
time.sleep(5)
target_link = self.driver.find_element(By.CLASS_NAME, "yidun_bg-img").get_attribute('src')
template_link = self.driver.find_element(By.CLASS_NAME, "yidun_jigsaw").get_attribute('src')
target_img = Image.open(BytesIO(requests.get(target_link).content))
template_img = Image.open(BytesIO(requests.get(template_link).content))
target_img.save('target.jpg')
template_img.save('template.png')
def crack_slider(self, distance):
slider = self.wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'yidun_slider')))
ActionChains(self.driver).click_and_hold(slider).perform()
ActionChains(self.driver).move_by_offset(xoffset=distance, yoffset=0).perform()
time.sleep(1)
ActionChains(self.driver).release().perform()
return 0
def add_alpha_channel(img):
""" 为jpg图像添加alpha通道 """
r_channel, g_channel, b_channel = cv2.split(img) # 剥离jpg图像通道
alpha_channel = np.ones(b_channel.shape, dtype=b_channel.dtype) * 255 # 创建Alpha通道
img_new = cv2.merge((r_channel, g_channel, b_channel, alpha_channel)) # 融合通道
return img_new
def handel_img(img):
imgGray = cv2.cvtColor(img, cv2.COLOR_RGBA2GRAY) # 转灰度图
imgBlur = cv2.GaussianBlur(imgGray, (5, 5), 1) # 高斯模糊
imgCanny = cv2.Canny(imgBlur, 60, 60) # Canny算子边缘检测
return imgCanny
def match(img_jpg_path, img_png_path):
# 读取图像
img_jpg = cv2.imread(img_jpg_path, cv2.IMREAD_UNCHANGED)
img_png = cv2.imread(img_png_path, cv2.IMREAD_UNCHANGED)
# 判断jpg图像是否已经为4通道
if img_jpg.shape[2] == 3:
img_jpg = add_alpha_channel(img_jpg)
img = handel_img(img_jpg)
small_img = handel_img(img_png)
res_TM_CCOEFF_NORMED = cv2.matchTemplate(img, small_img, 3)
value = cv2.minMaxLoc(res_TM_CCOEFF_NORMED)
value = value[3][0] # 获取到移动距离
return value
if __name__ == '__main__':
# 1. 打开chromedriver,试试下载图片
cs = CrackSlider()
cs.get_pic()
# 2. 对比图片,计算距离
img_jpg_path = 'target.jpg' # 读者可自行修改文件路径
img_png_path = 'template.png' # 读者可自行修改文件路径
distance = match(img_jpg_path, img_png_path)
distance = distance /480 * 345 + 12
# 3. 移动
cs.crack_slider(distance)
time.sleep(5)
网友评论