【发布时间】:2019-02-17 13:20:09
【问题描述】:
我正在尝试使用 Python 3.7 pyautogui 以管理员身份打开 CMD。我可以导航到开始菜单图标,键入“cmd”并按 ctrl + shift +enter 以在管理员模式下打开 cmd。
然后会弹出一条消息,提示是否以管理员身份打开。当我使用 pyautogui.press('left') 时,它没有按下左键。
try:
import pyautogui
import time
pyautogui.FAILSAFE = True
pyautogui.PAUSE = 0.5
mouseMovementDuration = 2 #every mouse movement will take 2 secs
intervalBetweenKeyPress = 0.5
def runCMDasAdmin():
x, y = pyautogui.locateCenterOnScreen(r'C:\\Users\\Saru\\Desktop\\PyAutoGUI\\images\\startmenu.png')
pyautogui.click(x=x, y=y,button='left',duration=mouseMovementDuration)
pyautogui.typewrite('cmd', interval=intervalBetweenKeyPress)
pyautogui.hotkey('ctrl', 'shift', 'enter')
pyautogui.press(['left','enter'],interval=intervalBetweenKeyPress)
print(pyautogui.size()) #It will give you the size of the screen
pyautogui.moveTo(x=1919,y=1079,duration=mouseMovementDuration)
pyautogui.moveTo(x=1,y=1,duration=mouseMovementDuration)
runCMDasAdmin()
except Exception as e:
print("Exception Raised------>",str(e))
我想使用 pyautogui 以管理员身份打开 cmd。请帮忙。
【问题讨论】:
-
如果我没记错的话,那些 UAC 窗口是在一个单独的层上打开的,通常会禁用其他程序的影响以防止恶意代码。因此,一旦 UAC 弹出,您将无法再控制屏幕/键盘。您可能想改用
subprocess,请在此处查看相关答案:stackoverflow.com/questions/47380378/…
标签: python python-3.x pyautogui