Home > OS >  mouse.up() not working after mouse.move()
mouse.up() not working after mouse.move()

Time:01-18

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. I use a local html canvas written from html and javascript, the code is from enter image description here

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()

CodePudding user response:

At the end of each iteration of "y" you issue a mouse up event. 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()

CodePudding user response:

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(). To test out if before drawing the first line, the mouse was in a specific position even before requesting the page. 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. 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()

enter image description here

  • Related