iOS - UI 自动化通过辅助功能标签获取文本字段?

发布于 2024-12-18 18:01:05 字数 516 浏览 0 评论 0原文

因此,在我的 nib 文件中,我启用了辅助功能并将文本字段的辅助功能标签设置为“txt” 我正在尝试根据辅助功能名称找到此文本文件并更改其文本。

var target = UIATarget.localTarget();
var application = target.frontMostApp(); 
var mainWindow = application.mainWindow();
mainWindow.logElementTree();

// This works
var textField = mainWindow.textFields()[0];

//this doesn't work
var textField = mainWindow.textFields()["txt"];

textField.setValue("Hello");

我做错了什么吗?如何根据可访问性标签找到标签?

编辑:

我也对其他解决方案持开放态度,我试图避免基于索引获取文本字段

So in my nib file i enabled accessibility and set the accessibility label of a textfield to "txt"
I am trying to find this textfiled based on accessibility name and change its text.

var target = UIATarget.localTarget();
var application = target.frontMostApp(); 
var mainWindow = application.mainWindow();
mainWindow.logElementTree();

// This works
var textField = mainWindow.textFields()[0];

//this doesn't work
var textField = mainWindow.textFields()["txt"];

textField.setValue("Hello");

Am I doing anything wrong? How can I find a label based on accessibility label?

EDIT:

I am open to other solutions as well, I am trying to avoid getting the textfield based on the index

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小傻瓜 2024-12-25 18:01:05

我有类似的问题,在代码中而不是在 nib 文件中设置辅助功能标签值解决了我的问题。例如,我需要在 UIAutomation 脚本中访问一个 UITextField,我必须在 viewDidLoad 方法中设置辅助功能标签值,如下所示。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    testTxtField.accessibilityLabel = @"myTxtBox";
}

下面是我的 UIAutomation 脚本

var target = UIATarget.localTarget();
var application = target.frontMostApp(); 
var mainWindow = application.mainWindow();
mainWindow.logElementTree();

// This works
//var textField = mainWindow.textFields()[0];

// Now, this work too.
var textField = mainWindow.textFields()["myTxtBox"];
textField.setValue("Hello");

UIALogger.logMessage("Text field:" + textField.label());

I have a similar problem and setting the accessibility label value in the code instead in the nib file solve my problem. For example, I have a UITextField that I need to access in my UIAutomation script, I would have to set the accessibility label value in the viewDidLoad method as shown below.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    testTxtField.accessibilityLabel = @"myTxtBox";
}

and below is my UIAutomation Script

var target = UIATarget.localTarget();
var application = target.frontMostApp(); 
var mainWindow = application.mainWindow();
mainWindow.logElementTree();

// This works
//var textField = mainWindow.textFields()[0];

// Now, this work too.
var textField = mainWindow.textFields()["myTxtBox"];
textField.setValue("Hello");

UIALogger.logMessage("Text field:" + textField.label());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文