Robot脚本Dialog对话框输入O/C触发OK/Cancel按钮问题求助
问题解决:Robot脚本输入O/C触发对话框OK/Cancel按钮
问题原因
你使用Dialogs库的Get Value From User弹出的GUI对话框,默认绑定了快捷键:O对应OK按钮,C对应Cancel按钮,输入字母O或C时会直接触发按钮,导致输入流程提前结束。
解决方法
方案1:改用控制台输入(推荐)
放弃GUI对话框,使用OperatingSystem库的Get Console Input方法直接在PyCharm控制台输入内容,彻底避免快捷键冲突问题。
修改测试用例部分代码:
*** Test Cases *** Get Credentials ${CO_username}= Get Console Input Enter account user name: Log To Console \n用户名:${CO_username} ${CO_password}= Get Console Input Enter account password: Log To Console \n密码:${CO_password}
方案2:自定义无快捷键对话框
如果必须使用GUI对话框,可以自定义一个取消默认快捷键绑定的输入框,通过Python关键字实现:
- 创建
CustomDialogs.py文件,写入以下代码:
import tkinter as tk from tkinter import simpledialog def get_value_without_shortcuts(prompt, default=""): root = tk.Tk() root.withdraw() dialog = simpledialog.askstring("Input", prompt, initialvalue=default, parent=root) # 移除OK/Cancel的Alt+O、Alt+C快捷键绑定 for btn in dialog.children.values(): if isinstance(btn, tk.Button): btn.unbind_all('<Alt-o>') btn.unbind_all('<Alt-c>') result = dialog.result root.destroy() return result
- 在Robot脚本的Settings部分导入自定义库:
*** Settings *** Library CustomDialogs.py
- 测试用例中调用自定义关键字:
*** Test Cases *** Get Credentials ${CO_username}= Get Value Without Shortcuts Enter account user name admin Log To Console \n${CO_username} ${CO_password}= Get Value Without Shortcuts Enter account password password Log To Console \n${CO_password}
内容的提问来源于stack exchange,提问作者5uck3rpunch
相关产品推荐
相关产品推荐

