leexgone / uiautomation-rs

The uiatomation-rs crate is a wrapper for windows uiautomation. This crate can help you make windows uiautomation API calls conveniently.
Apache License 2.0
88 stars 15 forks source link

How to find an element using Automation ID property #14

Closed codoid-repos closed 2 years ago

codoid-repos commented 2 years ago

How to find an element using Automation ID property?

Now we can use Name, Class Name, and Control Type properties to find elements.

leexgone commented 2 years ago

The UIMatcher supports to to define your custom filter:

  1. update to the latest version(v0.1.9);
  2. define a filter struct which implements the UIMatcherFilter traits.
  3. add your own filter to the matcher by UIMatcher.filter().
  4. you can also combine UIMatcherFilters with AndFilter & OrFilter.

Samples:

    struct FrameworkIdFilter(String);

    impl MatcherFilter for FrameworkIdFilter {
        fn judge(&self, element: &crate::UIElement) -> crate::Result<bool> {
            let id = element.get_framework_id()?;
            Ok(id == self.0)
        }
    }

    #[test]
    fn test_custom_search() {
        let automation = UIAutomation::new().unwrap();
        let matcher = automation.create_matcher().timeout(0).filter(Box::new(FrameworkIdFilter("Win32".into()))).depth(2);
        let element = matcher.find_first();
        assert!(element.is_ok());
        println!("{}", element.unwrap());
    }

Unfortunately, it's not a good idea to search element by Automation ID. I cannot get the Automation ID value correctly in my environment(windows 11). This property is not mandatory. AutomationId is not guaranteed to be stable across different releases or builds of an application. You can get more infomation from msdn: https://docs.microsoft.com/en-us/windows/win32/winauto/uiauto-automation-element-propids

codoid-repos commented 2 years ago

Thank you @leexgone It works.