WinAppDriver icon indicating copy to clipboard operation
WinAppDriver copied to clipboard

Simple and fast way to find element by value

Open testanna opened this issue 4 years ago • 10 comments

In our .Net-application we have a lot of problems when we can find an element in a tree or in a grid only by its value.

Now we have to use something like this:

            var treeItems = desktopSession.FindElementsByXPath("//Group/TreeItem/DataItem");
            WindowsElement docItem = null;
            foreach (var item in treeItems)
            {
                if (item.Text.Equals("Docs"))
                {
                    docItem = item;
                    break;
                }
            }

Finding items in this way is time consuming. Is there an easier and faster way to find an element by value? I need something like in Coded UI:

docItem.SearchProperties[WinCell.PropertyNames.Value] = "Docs";

testanna avatar Mar 02 '20 09:03 testanna

I don't think there's such a method. You may use LINQ instead of a foreach loop, it will make the code look elegant but behind the scenes LINQ will also use foreach. Maybe there's another control attribute which you could use with the GetAttribute to cobble together a logic. But essentially, this is just it.

naeemakram avatar Mar 02 '20 10:03 naeemakram

I only use WinAppDriver with classic forms so the following may not be applicable Language is C#

  public static OpenQA.Selenium.Appium.AppiumWebElement GetTreeNodeControl(OpenQA.Selenium.Appium.AppiumWebElement treeControl,
                                                                           String strNodeSelector)
  {
     string strControlType;
     OpenQA.Selenium.Appium.AppiumWebElement commandButton;
     string nodeSelector;

     nodeSelector = "//TreeItem[@Name=\"" + strNodeSelector + "\"]";

     try
     {
        commandButton = treeControl.FindElementByXPath(nodeSelector);
        strControlType = commandButton.TagName;
        if ("ControlType.TreeItem" != strControlType)
        {
           Assert.Fail("Control not a Tree item control");
        }
     }
     catch (Exception ex)
     {
        commandButton = null;

     }
     return (commandButton);
  }

I have deleted a candidate search child treeitems. Probably should have marked it as a candidate.

The root search is valid

Searching for child treeitems may be problematic

for the tree node ---> node ---> node

consider

treeItem1 = GetTreeNodeControl( treeControl, "node"); treeItem2 = GetTreeNodeControl( treeItem1, "node");

not certain whether treeItem2 will be correct It might just be treeeItem1

DLightstone avatar Mar 02 '20 14:03 DLightstone

@naeemakram thank you for answer. Therefore, it should be feature request. I think this case can be popular – to find element with specific value.

testanna avatar Mar 03 '20 07:03 testanna

@DLightstone In your case, you use xpath and specific value of element’s “Name”. In my case, I don’t have unique “Name” value, only “ValuePattern.Value”.

testanna avatar Mar 03 '20 07:03 testanna

@testanna I am confused by your reply. Name in my case corresponds to the text of the the node label for the tree item

For which Windows target environment was the application developed? (I have observed build dependencies which prevent proper XML generation for treeview controls) Which Tree View control are you using?

DLightstone avatar Mar 03 '20 22:03 DLightstone

@DLightstone

My tree's properties: Tree

DataItem in this tree: DataItem

testanna avatar Mar 04 '20 09:03 testanna

@testanna Now I am really confused.

I have associated TreeItems with Trees I have associated DataItems with Grids (display of database tables)

The Tree Control which I am using is definitely not the same as the one which you are using. I am using ClassName="WindowsForms10.SysTreeView32.app.0.141b42a_r6_ad1"

I do not know which application you used to prepare the 2 screen images. (Which application?) My guess is that the application you used prepares its display from the XML file that corresponds to the currently displayed screen. (Capture XML, process, filter and display). Debugging efforts are best conducted from the raw XML.

https://github.com/microsoft/WinAppDriver/issues/856 (you will have to modify it slightly it references libraries which only I have) indicates how you can capture the actual XML. . Insert a call to SaveSourceToFile(string window, string strFile) in the test script where you want the screen image captured as XML

If there is a tree present the xml will contain something similar to the text in the file below XML.txt

DLightstone avatar Mar 04 '20 11:03 DLightstone

@DLightstone I guess we use custom controls based on DevExpress, but I'm not sure. I use VisualUIAVerifyNative.exe in screenshots. But I don't really understand how this information can be helpful in this issue. The path for DataItem is somthing like: //Tree//TreeItem/DataItem. Neither TreeItem no DataItem have a unique value of the “Name” property. My entire conditions - I have element that I can find only by "ValuePattern.Value" (not by "Name") and I want to find it faster than in my example.

testanna avatar Mar 04 '20 12:03 testanna

@testanna

I do not have access to DevExpress, so there is not a lot I can do. Generate the XML, maybe VisualUIAVerifyNative.exe is concealing some useful information

DLightstone avatar Mar 04 '20 13:03 DLightstone

Would this be possible? (with Linq) var element = desktopSession.FindElementsByAccessibilityId("_tree").Where(e => e.GetAttribute("Value.Value") == [yourText]).FirstOrDefault();

motirek avatar Jul 20 '22 15:07 motirek