I'm having trouble getting interface elements by name in a UIAutomation script.
I have set up the accessibility panel for a text control:
And I know that I have the right parent view, as this code will work to set the field contents:
var view = UIATarget.localTarget().frontMostApp().mainWindow();
var textfields = view.textFields();
textfields[0].setValue("testuser");
Unfortunately, if I try to access the field by name, as the docs seem to indicate I should be able to do, I get an error:
var view = UIATarget.localTarget().frontMostApp().mainWindow();
var textfields = view.textFields();
textfields['foo'].setValue("testuser");
Cannot perform action on invalid element: UIAElementNil from target.frontMostApp().mainWindow().textFields()["foo"]
Does anybody have any idea why the find-by-name functionality doesn't seem to be working here for me?
Thanks for any insight!
If you are working with the accessibility Layer, you must to enable it in the Device/Simulator:
Settings-> General-> Accessibility-> Accessibility Inspector-> ON
It appears that UI Automation looks for the element's name in the Title field rather than the documented Accessibility Label field; at least, that's my experience. Adding a Title to your UI elements (via Attributes) should result in the desired behaviour.
Update: It now appears, after further experimentation, that the field to edit may be the Accessibility Identifier (not Label), which AFAICT can only be set programmatically.
According to UI Automation Reference Guide
UIAElementArray section, you may want to try like this:
var textfields = view.textFields();
textfields.withName("foo")[0].setValue("testuser");
来源:https://stackoverflow.com/questions/9365050/trouble-getting-elements-by-name-from-uiaelementarray-in-uiautomation