web-dev-qa-db-ja.com

セレンを介した人間のようなマウスの動き

ストーリー:

Google ReCaptchaのようなキャプチャを解決する方法の1つは、人間のマウスの動作を模倣する:動き、ホバリング、クリックを試みることです。

一部のユーザーが報告 マウスの動きを B-スプライン曲線 として機能させた。

質問:

Seleniumを介してBスプライン軌道に従って特定の要素にマウスを移動する方法は?


通常のbrowser.actions().mouseMove(Elm).perform();は、要素にまっすぐに「ジャンプ」して、あまりにも速くなることに注意してください。私の理解では、Bスプライン軌道の数学モデルに従って、移動速度を遅くし、点から点へスムーズに「ジャンプ」することです。

Protractor/JavaScriptを使用していますが、質問は本当に言語に依存しません。私がキャプチャを解決しようとしていないこと、または「キャプチャの解決は、あちこちの利用規約に違反する新しい悪のボットを作る」スペースに貢献しているわけではないことに注意してください。私は、テスト自動化の分野でより多くのスキルを習得したいと思っています。

38
alecxe

scipy.interpolateを使用して、この question で見られるようにBスプライン曲線を補間できます。

ここでは、B-splineのいずれかの例を使用して、xおよび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でマウスカーソルを移動できます。

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)
25
Guilherme