find window using attribute className

Aug 18, 2010 at 2:57 PM
I have a window under a app which does not have a Name/Title or ID. Its has a className. Is it possible to find a window using className. i also used user32.dll and pinvoke.net , i was able to list the className property. Any help will be appreciated. Thanks -M.
Aug 18, 2010 at 5:11 PM

Hi

since 0.20

 

Ability to search for UI Item(s) based on any native (UIAutomation) property values.

Snippet

Button button = window.Get<Button>( SearchCriteria.ByNativeProperty( AutomationElement.ClassNameProperty, "Button" ) );

 

 

Throndorin

Aug 19, 2010 at 5:07 PM
Thanks Throndorin for answering. I eventually got this method to get windows using title or className as identifiers

/// <summary>

/// This method returns the handle to a window with the specified title if its exits under the given procees.

/// </summary>

/// <param name="processName">Name of the parent process of the windoe.</param>

/// <param name="windowIdentifier">The expected window title to className</param>

/// <returns>Window</returns>

private static Window GetWindow(string processName, string windowIdentifier)

{

StringBuilder feedbackText = new StringBuilder();

try

{

//Check is the Process if responding. If the process is in an unresponsive state, app.GetWindow() hangs.

if (!ProcessHelper.IsProcessResponding(processName)) {

return null;

}

windowIdentifier = windowIdentifier.ToLower().Trim();

//Get all windows under the given process

int processID = ProcessHelper.ReturnProcessIDs(processName);

Application app = Application.Attach(processID);

List<Window> windowList = app.GetWindows();

if (windowList == null)

{

log.Error("Could not find any Window under process [" + processName + "]");

return null;

}

//Iterate through the window list and find the window.

for (int i = 0; i < windowList.Count; i++) {

// look for the window using title

String actualWindowTitle = windowList[i].Title.ToLower().Trim();

if (string.Equals(actualWindowTitle, windowIdentifier))

return windowList[i];

// now look for the window using className

AutomationElement ae = windowList[i].AutomationElement;

String actualClassName = ae.GetCurrentPropertyValue(AutomationElement.ClassNameProperty).ToString();

if (String.Equals(actualClassName.ToLower().Trim(), windowIdentifier))

return windowList[i];

// now log for error

if (i == 0)

{

feedbackText.AppendLine("/n List of Windows for process [" + processName + "]");

feedbackText.AppendLine("window number, Title , ClassName");

}

feedbackText.AppendLine(i + ". " + actualWindowTitle + " " + actualClassName);

}

} catch (Exception e) {

log.Error("EXCEPTION: \n" + e.Message);

}

log.Error("Could not find Window under process [" + processName + "] for title or className [" + windowIdentifier + "]");

Feedback(feedbackText.ToString());

return null;

}

Parameshwaran Murli
Extn: 805-690-3410 AIM UID: ursmurli



From: Throndorin [mailto:notifications@codeplex.com]
Sent: Wednesday, August 18, 2010 10:11 AM
To: Parameshwaran Murli
Subject: Re: find window using attribute className [white:223972]

From: Throndorin

Hi

since 0.20

Ability to search for UI Item(s) based on any native (UIAutomation) property values.

Snippet

Button button = window.Get<Button>( SearchCriteria.ByNativeProperty( AutomationElement.ClassNameProperty, "Button" ) );

Throndorin