Я нашел ссылку, которая не решает мою проблему. Вот мой код:
Код: Выделить всё
import asyncio
import random
from playwright.async_api import async_playwright, Page
async def move_cursor_randomly(page: Page, duration: int = 10):
"""
Moves cursor randomly within the viewport for the specified duration.
"""
viewport_size = page.viewport_size
width = viewport_size["width"]
height = viewport_size["height"]
end_time = asyncio.get_event_loop().time() + duration
while asyncio.get_event_loop().time() < end_time:
x = random.randint(0, width)
y = random.randint(0, height)
await page.mouse.move(x, y, steps=random.randint(10, 30))
await draw_cursor_trail(page, x, y)
await asyncio.sleep(random.uniform(1, 3)) # to mimic randomness
async def draw_cursor_trail(page: Page, x, y):
"""
supposed to draw a cursor trail at the specified coordinates.
"""
await page.evaluate("""
(x, y) => {
const trail = document.createElement('div');
trail.style.position = 'absolute';
trail.style.left = `${x}px`;
trail.style.top = `${y}px`;
trail.style.width = '10px';
trail.style.height = '10px';
trail.style.borderRadius = '50%';
trail.style.backgroundColor = 'blue';
trail.style.zIndex = '9999';
document.body.appendChild(trail);
setTimeout(() => {
trail.remove();
}, 1000);
}
""", arg=(x, y))
P.S -> в верхней части экрана мигает синяя точка, и я подозреваю, что это и есть след. Если да, то почему он не следует за курсором. Я слишком запутался!!
Подробнее здесь: https://stackoverflow.com/questions/791 ... -movements
Мобильная версия