通过 Selenium 进行类似人类的鼠标移动 [英] Human-like mouse movements via Selenium

查看:46
本文介绍了通过 Selenium 进行类似人类的鼠标移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

故事:

解决验证码的方法之一,例如 Google ReCaptcha,是尝试模仿人类鼠标操作:移动、悬停和点击.

One of the approaches to solve captchas, like Google ReCaptcha, is to try to imitate the human mouse actions: movements, hovering and clicks.

一些用户报告将鼠标移动作为B-spline 曲线 为他们工作.

Some users reported that making mouse moves as B-spline curves worked for them.

问题:

如何通过 Selenium 将鼠标移动到遵循 B 样条轨迹的特定元素?

How to move the mouse to a particular element following the B-spline trajectory via Selenium?

请注意,常规的 browser.actions().mouseMove(elm).perform(); 会直接跳转"到元素并且太快.我的理解是减慢运动速度,按照B样条轨迹的数学模型从一个点到另一个点平滑地跳跃".

Note that the regular browser.actions().mouseMove(elm).perform(); would "jump" to the element straight and far too quickly. My understanding is that it is a matter of slowing down the movement speed, "jumping" from point to point smoothly following the mathematical model for the B-spline trajectory.

我们正在使用 Protractor/JavaScript,但问题确实与语言无关.请注意,我并不是要解决验证码问题,也不是为解决验证码问题制造新的邪恶机器人在各处违反使用条款"的空间做出贡献.我只是好奇并渴望在测试自动化领域获得更多技能.

推荐答案

你可以使用 scipy.interpolate 来插入 B 样条曲线,就像你在这个 问题.

You can use scipy.interpolate to interpolate B-spline curves like you can see in this question.

这里我将使用 B-spline 示例之一来获取 xy 的值:

Here I'll use one of the B-spline examples to get values to x and y:

import numpy as np
import scipy.interpolate as si

# Curve base:
points = [[0, 0], [0, 2], [2, 3], [4, 0], [6, 3], [8, 2], [8, 0]];
points = np.array(points)

x = points[:,0]
y = points[:,1]


t = range(len(points))
ipl_t = np.linspace(0.0, len(points) - 1, 100)

x_tup = si.splrep(t, x, k=3)
y_tup = si.splrep(t, y, k=3)

x_list = list(x_tup)
xl = x.tolist()
x_list[1] = xl + [0.0, 0.0, 0.0, 0.0]

y_list = list(y_tup)
yl = y.tolist()
y_list[1] = yl + [0.0, 0.0, 0.0, 0.0]

x_i = si.splev(ipl_t, x_list) # x interpolate values
y_i = si.splev(ipl_t, y_list) # y interpolate values

使用 xy 的值,您可以使用 ActionChains 移动鼠标光标:

With values of x and y, you can move the mouse cursor with ActionChains:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

url = "https://codepen.io/falldowngoboone/pen/PwzPYv"
driver = webdriver.Chrome(executable_path="/home/selenium/chromedriver2.25")
driver.get(url)

action =  ActionChains(driver);

startElement = driver.find_element_by_id('drawer')

# First, go to your start point or Element:
action.move_to_element(startElement);
action.perform();

for mouse_x, mouse_y in zip(x_i, y_i):
    action.move_by_offset(mouse_x,mouse_y);
    action.perform();
    print(mouse_x, mouse_y)

这篇关于通过 Selenium 进行类似人类的鼠标移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆