简体   繁体   English

mouse.up() 在 mouse.move() 之后不工作

[英]mouse.up() not working after mouse.move()

I'm writing a test in Playwright Python and pytest to see if the auto mouse movements can be simulated to look more like of a real user's.我正在 Playwright Python 和 pytest 中编写测试,看看是否可以模拟鼠标的自动移动,使其看起来更像真实用户的移动。 I use a local html canvas written from html and javascript, the code is from here .我用的是从html和javascript写的本地html canvas,代码来自这里 The mouse is supposed to move to point (400,50) in the browser before the HTML canvas file is requested (In the real function, the starting point will instead will randomized. Otherwise, it will always start at (0,0) which would make it look more like a bot).在请求 HTML canvas 文件之前,鼠标应该移动到浏览器中的点 (400,50) (在真实的 function 中,起点将随机化。否则,它将始终从 (0,0) 开始会让它看起来更像一个机器人)。 When the canvas is open, it's supposed to draw lines from left to right using WindMouse algorithm with the same x-values for start and end points, respectively.当 canvas 打开时,它应该使用WindMouse 算法从左到右绘制线,起点和终点的 x 值分别相同。 There shouldn't be any lines connected between the lines, except for the one from the starting point to the first line.线与线之间不应该有任何线连接,除了从起点到第一条线的那条线。 This should be because after initiating to hold down the mouse's left button with page.mouse.down() , and then actually drawing with page.mouse.move() from x=100 to x=1200 with different y-values in range 100 to 1000, the mouse should release outside of the loop with page.mouse.up() .这应该是因为在开始使用page.mouse.down()按住鼠标左键之后,然后实际使用page.mouse.move()x=100绘制到x=1200 ,并在 100 范围内使用不同的 y 值到 1000,鼠标应该在page.mouse.up()循环之外释放。

As seen in the image below, that's not what happened.如下图所示,事实并非如此。 Instead the page.mouse.up() doesn't seem to execute after page.mouse.down() and page.mouse.move() .相反, page.mouse.up()似乎没有在page.mouse.down()page.mouse.move()之后执行 I have researched and found that it might be because when the mouse left button has been held down for a certain amount of time, the browser will recognize the action as a mouse drag instead.我研究了一下,可能是因为当鼠标左键按住一定时间后,浏览器会把这个动作识别为鼠标拖动。 If this is the case, how do you disable the browser's ability to automatically switch mouse action recognition;如果是这种情况,如何禁用浏览器自动切换鼠标动作识别的能力; in this case, it would be to disable it from automatically recognizing page.mouse.down() and page.mouse.move() after a certain amount of time as a mouse drag?在这种情况下,它会禁止它在一定时间后自动识别page.mouse.down()page.mouse.move()作为鼠标拖动? And if this is not the case, how do you fix this problem with Playwright page.mouse.up() ?如果不是这种情况,您如何使用 Playwright page.mouse.up()解决此问题? 在此处输入图像描述

Please have a look at the code:请看一下代码:

def test_drawing_board():
    rel_path = r"/mats/drawing_board.html"
    file_path = "".join([r"file://", os.getcwd(), rel_path])
    with sync_playwright() as playwright:
        # Fetch drawing board
        browser = playwright.chromium.launch(headless=False, slow_mo=0.1)
        page = browser.new_page()
        page.mouse.move(400,50) # Place mouse in a random position in the browser before fetching the page
        page.goto(file_path)

        #Move mouse
        start_point = 100
        x = 1200
        for y in range(100, 1000, 100):
            # Generate mouse points
            points = []
            wm(start_point, y, x, y, M_0=15, D_0=12, move_mouse=lambda x, y: points.append([x, y]))

            # Draw
            page.mouse.down()
            for point in points:
                page.mouse.move(point[0], point[1])
            page.mouse.up()

At the end of each iteration of "y" you issue a mouse up event.在“y”的每次迭代结束时,您都会发出鼠标弹起事件。 However, you dont issue a mouse move event to the start of the new line before putting the mouse down again.但是,在再次放下鼠标之前,您不会向新行的开头发出鼠标移动事件。

Try the below code:试试下面的代码:

    for y in range(100, 1000, 100):
        # Generate mouse points
        points = []
        wm(start_point, y, x, y, M_0=15, D_0=12, move_mouse=lambda x, y: points.append([x, y]))

        page.mouse.up()
        # Move mouse to start of new line
        page.mouse.move(points[0][0], points[0][1])
        # Mouse back down
        page.mouse.down()
        #Iterate the remaining points
        for point in points[1:]:
            page.mouse.move(point[0], point[1])
        page.mouse.up()

Within the # Draw block it should include a page.mouse.move(start_point, y) to move to the beginning of the line before every draw as @tomgalfin suggested, and then page.mouse.down() .# Draw block中,它应该包含一个page.mouse.move(start_point, y)以按照@tomgalfin 的建议在每次绘制之前移动到行的开头,然后page.mouse.down() To test out if before drawing the first line, the mouse was in a specific position even before requesting the page.为了测试在绘制第一行之前,鼠标是否在特定的 position 中,甚至在请求页面之前。 I wanted to confirm the initial mouse position with a line drawn between it and the first line's beginning position. This is achieved with adding a page.mouse.down() before the # Move mouse loop as edited.我想确认初始鼠标 position,并在它和第一行的开头 position 之间画一条线。这是通过在# Move mouse loop as edited 之前添加page.mouse.down()来实现的。 Here's the solved code and the results这是已解决的代码和结果

def test_drawing_board():
    rel_path = r"/mats/drawing_board.html"
    file_path = "".join([r"file://", os.getcwd(), rel_path])
    with sync_playwright() as playwright:
        # Fetch drawing board
        browser = playwright.chromium.launch(headless=False, slow_mo=0.1)
        page = browser.new_page()
        page.mouse.move(400,50) # Place mouse in a random position in the browser before fetching the page
        page.goto(file_path)

        # Start points
        start_point = 100
        x = 1200

        # Move mouse
        page.mouse.down()
        for y in range(100, 1000, 100):
            # Generate mouse points
            points = []
            wm(start_point, y, x, y, M_0=15, D_0=12, move_mouse=lambda x, y: points.append([x, y]))

            # Draw
            page.mouse.move(start_point, y)
            page.mouse.down()
            for point in points:
                page.mouse.move(point[0], point[1])
            page.mouse.up()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM