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)
CodePudding user response:
It looks like you're trying to use the pyautogui library to check the mouse position while also using Playwright to control the mouse. However, pyautogui is not designed to work in conjunction with Playwright, and it may be causing conflicts.
Instead of using pyautogui to check the mouse position, you can use Playwright's page.mouse.position property to check the current position of the mouse. Here's an example of how you could update your code to use Playwright's mouse position:
import pytest
from playwright.sync_api import sync_playwright
def test_simple_move():
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/")
mouse_start_position = page.mouse.get_position()
print(mouse_start_position)
page.mouse.move(100,200)
mouse_final_position = page.mouse.get_position()
print(mouse_final_position)
With this code you should be able to see the different coordinates between the start and final mouse positions. Also, you don't need to use page.mouse.up() and page.mouse.down() before and after the page.mouse.move(100,200) unless you are trying to simulate a click event.
CodePudding user response:
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:
- Launch the code
- While the delay of 10 seconds is going, move the cursor to another point on the screen.
- Wait for the code to finish.
- You will now see how the points are different. Here are my points