I have a python script which uses selenium to automate web page, drawing focus away from the terminal where user input is required.
Is there anyway in python to switch focus back to the terminal, programmatically?
I will be running my program in the Windows Command Prompt on Windows 7, if it matters, but a cross platform answer would be most useful.
Attempts
Looking at the pywin32
package bindings for the win32 API I have the following:
import win32console
import win32gui
from selenium import webdriver as wd
d = wd.Firefox()
win32gui.SetFocus(win32console.GetConsoleWindow())
win32gui.FlashWindow(win32console.GetConsoleWindow(), False)
input('Should have focus: ')
SetFocus
causes the error pywintypes.error: (5, 'SetFocus', 'Access is denied.')
due to Microsoft removing the ability to take focus from another application.
FlashWindow
appears to do nothing.
win32gui.SetFocus(win32console.GetConsoleWindow())
, I get the following error: "pywintypes.error: (5, 'SetFocus', 'Access is denied.')". It seems you don't.. Any idea why? (I run Python 2.7+)