I'm writing a test code to check if the mouse is moving in the Playwright browser. I used pyautogui in this case to locate current mouse position but that might be the problem, so I was wondering if there is a similar method for Playwright Python?
Please have a look at the code below. It prints out the same coordinates for the start and final mouse positions, which means there's no mouse movement. I have tried adding page.mouse.up() and page.mouse.down() before and after the page.mouse.move(100,200) as shown on the official Playwright Docs page but to no avail. How do you move mouse with Playwright Python?
import pytest
import pyautogui
from playwright.sync_api import sync_playwright
def test_simple_move():
mouse_start_position = pyautogui.position()
print(mouse_start_position)
with sync_playwright() as playwright:
browser = playwright.chromium.launch(headless=False, slow_mo=10)
page = browser.new_page()
page.goto(r"http://www.uitestingplayground.com/")
page.mouse.move(100,200)
mouse_final_position = pyautogui.position()
print(mouse_final_position)
I see what the problem here is. It does have to do with pyautogui and playwright not functioning together correctly. I tried searching around to find how to actually get the current mouse position with playwright, but I did not find anything.
So, here is my conclusion:
How did I figure out the problem, and what actually is the problem?
The problem here is that pyautogui actually controls the computer's mouse in this case. It seems like playwright only controls that of the browser. If you add a time.sleep(10) (In order to actually see if the cursor has changed position and to have time to move the cursor at a different position) at the end of the function like so:
def test_simple_move():
mouse_start_position = pyautogui.position()
print(mouse_start_position)
with sync_playwright() as playwright:
browser = playwright.chromium.launch(headless=False, slow_mo=10)
page = browser.new_page()
page.goto(r"http://www.uitestingplayground.com/")
page.mouse.move(100,400)
mouse_final_position = pyautogui.position()
print(mouse_final_position)
time.sleep(10)
You will then see that the actual cursor does not move. It seems like what playwright is doing here is just moving a "virtual" cursor in its browser. In fact, what I did to prove this was:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With