c# - TextBox 在值模式中抛出不支持的模式

标签 c# c#-4.0 automation ui-automation microsoft-ui-automation

有谁知道如何在运行时使用 UI Automation 和 .Net 启用 TextBox 后在 TextBox 中设置值?

有关详细信息:最初加载应用程序时,文本框被禁用。使用自动化切换复选框后,文本框被启用。但使用自动化是无法访问的。 我尝试了以下方法:

PropertyCondition parentProcCond = new PropertyCondition(AutomationElement.ProcessIdProperty, processes[0].Id);
Condition chkCondition = new AndCondition(
                            new PropertyCondition(AutomationElement.IsEnabledProperty, true),
                            new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.CheckBox),
                            new PropertyCondition(AutomationElement.NameProperty, chkName));
//Find Elements
var parentElement = AutomationElement.RootElement.FindFirst(TreeScope.Children, parentProcCond);

var chkUseMyAccountElement = parentElement.FindFirst(TreeScope.Descendants, chkCondition);
TogglePattern pattern = chkUseMyAccountElement.GetCurrentPattern(TogglePattern.Pattern) as TogglePattern;
ToggleState state = pattern.Current.ToggleState;
if (state == ToggleState.On)
{
    pattern.Toggle();
}

Condition txtDomainCondition = new AndCondition(
                           new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Text),
                           new PropertyCondition(AutomationElement.NameProperty, txtDomain)
                           );

var txtConditionElement = parentElement.FindFirst(TreeScope.Descendants, txtDomainCondition);
ValuePattern valuetxtDomain = txtConditionElement.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
valuetxtDomain.SetValue("US");

它在 ValuePattern 行中抛出不支持的模式。

最佳答案

我找到了答案。

将控件类型修改为编辑控件类型,而不是文本控件类型。它正在工作。

 Condition txtDomainCondition = new AndCondition(
                       new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit),
                       new PropertyCondition(AutomationElement.NameProperty, txtDomain)
                       );

关于c# - TextBox 在值模式中抛出不支持的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39465019/

相关文章:

android - 即使找不到元素或在 appium for android 中等待,如何继续

c# - 使用网格中的文本框和带有属性的标签设计用户控件

c# - 如何连接 table1 - table2 和 table1 - table3 并返回 DTO 变量

c# - 国际化 HelloWorld 程序 .NET

c#-4.0 - 另一个 'Length of the data to decrypt is invalid.' 错误

c# - 向上转换为通用类型

c# - 为项目还原 NuGet 包失败... : Could not find a part of the path

c# - 为什么我的列表中的成员会被所述列表的最后一个成员覆盖?

html - 不能通过 Mac OS X "open"命令使用 mailto 包含主题和正文

linux - 查找并删除文件但不是特定路径