Unable to get button control in OpenFileDialog using UIAutomation

  • Thread starter Thread starter Adinb007
  • Start date Start date
A

Adinb007

Guest
Folks,

I need to to click on "Open" button however this control is not available through UIAutomation approach.

Below is the code.

public bool PressButton(string parentclassname, string windowTitle, string buttonClassName,string buttonName)
{
windowTitle = windowTitle.ToLower();

bool result = false;

try
{
IntPtr parentHandle = FindWindow(parentclassname, windowTitle);

var parentElements = AutomationElement.FromHandle(parentHandle).FindAll(TreeScope.Children, Condition.TrueCondition);
foreach (AutomationElement parentElement in parentElements)
{
if (parentElement.Current.ClassName !="")
{
var childElements = parentElement.FindAll(TreeScope.Children, Condition.TrueCondition);

foreach (AutomationElement childElement in childElements)
{
if(childElement.Current.ClassName == "Button")
{
AutomationElement aeButton = childElement.FindFirst(System.Windows.Automation.TreeScope.Element, new System.Windows.Automation.PropertyCondition(System.Windows.Automation.AutomationElement.NameProperty, childElement.Current.Name));

if (aeButton != null)
{
try
{
InvokePattern vpattern = (InvokePattern)aeButton.GetCurrentPattern(InvokePattern.Pattern);
if (vpattern != null)
{
System.Threading.Thread.Sleep(1000);
vpattern.Invoke();
result = true;
//break;
}
}
catch
{
result = false;
continue;
}
}
}
if (result)
{
break;
}
}
}
}
catch (Exception ex)
{
result = false;
}
return result;
}

1574015.png


Aditya Sharma

Continue reading...
 
Back
Top