asweigart / pyautogui

A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.
BSD 3-Clause "New" or "Revised" License
10.45k stars 1.26k forks source link

Enter key not being recognized by web page #862

Open gds13z240 opened 5 months ago

gds13z240 commented 5 months ago

My company has this internal web page search database. I had previously set up pyautogui script in Windows 10 to enter keywords into the web page search box, press enter to search, copy results, and fill out the forms. On a recent update to that page, it no longer accepts the enter key from pyautogui.

After a lot of troubleshooting, I believe the issue is with the web page and how pyautogui simulates the enter key. I inspected the page search box with the developer console in Chrome and Edge with monitorEvents($0). The pyautogui enter key input is recognized as a keyboard event, but it’s different from what the web page was expecting.

See below. Also I’m not an experienced programmer…

Physical keyboard press for ‘enter’ KeyBoardEvent{isTrusted: true, key: 'Enter', code: 'Enter',Location: 0, ctrlKey: false, …} Pyautogui keyboard ‘enter’ KeyBoardEvent{ isTrusted: true, key: 'Enter', code: '', Location: 0, ctrlKey: false, ...}

The key value pairs are almost identical except for code: ‘Enter’ vs code: “” (empty string)’

When I debugged the page further, there is a conditional statement in the js script that checks the key value for code === 'Enter'

const KeyDownPress = (e) => { //e = some code
   if(e.code === 'Enter' || e.code === 'NumPadEnter'){
       // code block here would proceed to execute the search
   }
}

When I watch that conditional run with my python script, the result is always false. The KeyboardEvent received from pyauto doesn't contain code: 'enter', so the search doesn't occur. When I physically press enter or use windows virtual keyboard, that conditional is true and the search proceeds.

How do I get pyautogui to send the correct keyboard input for enter? I have no control over the programming of that web page. Therefore, I just do a workaround by having the mouse move to the search button and execute a click. This works for now but I got other scripts that will need to be edited due to this issue.