GUI Automation of policy change for logoff button. pywinauto.findwindows.ElementNotFoundError: error. How to switch context?
GUI Automation of policy change for logoff button. pywinauto.findwindows.ElementNotFoundError: error. How to switch context?
我正在自动执行更改注销按钮策略的步骤。涉及的步骤是:
使用 gpedit.msc
打开本地组策略
Select "Start Menu and Taskbar" 从左窗格中用户配置 > 管理模板的下拉菜单中
在右窗格中,双击 "Change Start Menu power button"
Select单选按钮"Enabled"
从选项的下拉菜单中:Select "Log Off"
我已经完成了第三步,但是我在映射第二步的 "Change Start Menu Power Button" 时遇到了问题。我的代码如下:
from pywinauto import Application
Application().start(r'mmc gpedit.msc')
app = Application(backend="uia").connect(path='mmc.exe')
#app.LocalGroupPolicyEditor.dump_tree()
Admin_template = app.LocalGroupPolicyEditor.child_window(title="User
Configuration", control_type="TreeItem").child_window(title="Administrative
Templates", control_type="TreeItem") # since there are same templates
Admin_template.double_click_input() # it expands the subtree
#Admin_template.dump_tree()
Start_menu = Admin_template.child_window(title="Start Menu and Taskbar",
control_type="TreeItem").double_click_input()
Start_menu.dump_tree()
#Admin_template.child_window(title="Start Menu and Taskbar",
control_type="TreeItem").dump_tree()
#Change_start_menu = Start_menu.child_window(title="Change Start Menu power
#button", control_type="MenuItem").double_click_input()
#Change_start_menu.dump_tree()
我在右窗格中查找和映射元素时遇到问题。另外,当我使用 Start_menu.dump_tree()
时,只显示 "Notification" 个元素。然而,剩下的,包括 "Change Start Menu power button," 是我下一步要双击的内容。
感谢您的帮助。谢谢
这有点棘手,但这应该可以完成工作(它会执行您列出的所有步骤 - 按确定 - 并关闭程序):
import pywinauto
pywinauto.Application().start(r'mmc gpedit.msc')
app = pywinauto.Application(backend="uia").connect(path='mmc.exe')
admin_template = app.LocalGroupPolicyEditor.child_window(title="User Configuration", control_type="TreeItem").child_window(title="Administrative Templates", control_type="TreeItem")
admin_template.double_click_input()
start_menu = admin_template.child_window(title="Start Menu and Taskbar", control_type="TreeItem")
start_menu.double_click_input()
option_list = app.LocalGroupPolicyEditor.child_window(auto_id="12786", control_type="List")
# Just select any of the first options to change the focus to the list.
first_elem = option_list.child_window(title="Add Search Internet link to Start Menu", control_type="ListItem")
first_elem.click_input()
# Used to scroll down the window so that the wanted option becomes visible.
pywinauto.keyboard.send_keys("cccc")
option = option_list.child_window(title="Change Start Menu power button", control_type="ListItem")
option.double_click_input()
pop_up = app.LocalGroupPolicyEditor.child_window(auto_id="tableLayoutFullForm", control_type="Pane")
radio = pop_up.child_window(title="Enabled", auto_id="radioButtonEnabled", control_type="RadioButton")
radio.click_input()
drop_down = pop_up.child_window(title="Choose one of the following actions", auto_id="dropDownListChoose one of the following actions", control_type="ComboBox")
drop_down.click_input()
# 'Hack' to first select the Restart option and then the next option after that which starts with l (=Log off).
# This ensures that the correct setting gets set despite of what the setting was before.
pywinauto.keyboard.send_keys("rl{ENTER}")
ok = pop_up.child_window(title="OK", auto_id="buttonOK", control_type="Button")
ok.click_input()
app.kill()
确保以管理员身份运行此脚本,否则它将失败。
如果您对代码有任何疑问,请随时询问 :)
编辑:
如果您运行正在使用 <0.6.0 的 pywinauto 版本,则必须将 pywinauto.keyboard.send_keys()
的两次出现(第 19 和 34 行)替换为:
pywinauto.SendKeysCtypes.SendKeys()
如果这不起作用,您可以尝试:
pywinauto.keyboard.SendKeys()
我正在自动执行更改注销按钮策略的步骤。涉及的步骤是:
使用 gpedit.msc
打开本地组策略
Select "Start Menu and Taskbar" 从左窗格中用户配置 > 管理模板的下拉菜单中
在右窗格中,双击 "Change Start Menu power button"
Select单选按钮"Enabled"
从选项的下拉菜单中:Select "Log Off"
我已经完成了第三步,但是我在映射第二步的 "Change Start Menu Power Button" 时遇到了问题。我的代码如下:
from pywinauto import Application
Application().start(r'mmc gpedit.msc')
app = Application(backend="uia").connect(path='mmc.exe')
#app.LocalGroupPolicyEditor.dump_tree()
Admin_template = app.LocalGroupPolicyEditor.child_window(title="User
Configuration", control_type="TreeItem").child_window(title="Administrative
Templates", control_type="TreeItem") # since there are same templates
Admin_template.double_click_input() # it expands the subtree
#Admin_template.dump_tree()
Start_menu = Admin_template.child_window(title="Start Menu and Taskbar",
control_type="TreeItem").double_click_input()
Start_menu.dump_tree()
#Admin_template.child_window(title="Start Menu and Taskbar",
control_type="TreeItem").dump_tree()
#Change_start_menu = Start_menu.child_window(title="Change Start Menu power
#button", control_type="MenuItem").double_click_input()
#Change_start_menu.dump_tree()
我在右窗格中查找和映射元素时遇到问题。另外,当我使用 Start_menu.dump_tree()
时,只显示 "Notification" 个元素。然而,剩下的,包括 "Change Start Menu power button," 是我下一步要双击的内容。
感谢您的帮助。谢谢
这有点棘手,但这应该可以完成工作(它会执行您列出的所有步骤 - 按确定 - 并关闭程序):
import pywinauto
pywinauto.Application().start(r'mmc gpedit.msc')
app = pywinauto.Application(backend="uia").connect(path='mmc.exe')
admin_template = app.LocalGroupPolicyEditor.child_window(title="User Configuration", control_type="TreeItem").child_window(title="Administrative Templates", control_type="TreeItem")
admin_template.double_click_input()
start_menu = admin_template.child_window(title="Start Menu and Taskbar", control_type="TreeItem")
start_menu.double_click_input()
option_list = app.LocalGroupPolicyEditor.child_window(auto_id="12786", control_type="List")
# Just select any of the first options to change the focus to the list.
first_elem = option_list.child_window(title="Add Search Internet link to Start Menu", control_type="ListItem")
first_elem.click_input()
# Used to scroll down the window so that the wanted option becomes visible.
pywinauto.keyboard.send_keys("cccc")
option = option_list.child_window(title="Change Start Menu power button", control_type="ListItem")
option.double_click_input()
pop_up = app.LocalGroupPolicyEditor.child_window(auto_id="tableLayoutFullForm", control_type="Pane")
radio = pop_up.child_window(title="Enabled", auto_id="radioButtonEnabled", control_type="RadioButton")
radio.click_input()
drop_down = pop_up.child_window(title="Choose one of the following actions", auto_id="dropDownListChoose one of the following actions", control_type="ComboBox")
drop_down.click_input()
# 'Hack' to first select the Restart option and then the next option after that which starts with l (=Log off).
# This ensures that the correct setting gets set despite of what the setting was before.
pywinauto.keyboard.send_keys("rl{ENTER}")
ok = pop_up.child_window(title="OK", auto_id="buttonOK", control_type="Button")
ok.click_input()
app.kill()
确保以管理员身份运行此脚本,否则它将失败。
如果您对代码有任何疑问,请随时询问 :)
编辑:
如果您运行正在使用 <0.6.0 的 pywinauto 版本,则必须将 pywinauto.keyboard.send_keys()
的两次出现(第 19 和 34 行)替换为:
pywinauto.SendKeysCtypes.SendKeys()
如果这不起作用,您可以尝试:
pywinauto.keyboard.SendKeys()