Hi,
My application find the currently open URL from chrome when user press "enter" key.
But actually when I press the "enter" key sometime application is crashed and all time it doesn't give me a opened URL.
And most stressful problem is application works good if I set debug point before or at this
AutomationElement elm = AutomationElement.FromHandle(new IntPtr(handle));
line.
If trying without debug point its not working.
And yes main thing is that there is no any exception.
here I attach the full code of my method which return the URL of chrome opened tab.
private string GetCurrentChromeUrl()
{
try
{
string url = null;
int handle = GetForegroundWindow();
foreach (Process P in Process.GetProcessesByName("chrome"))
{
if(P.MainWindowHandle.ToInt32() > 0)
{
handle = P.MainWindowHandle.ToInt32();
break;
}
}
AutomationElement elm = AutomationElement.FromHandle(new IntPtr(handle));
AutomationElement elmUrlBar = elm.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
if (elmUrlBar != null)
{
AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns();
if (patterns.Length > 0)
{
ValuePattern val = (ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0]);
url = val.Current.Value;
}
}
return url;
}
catch (Exception e1)
{
return "";
}
}
I'm very upset by facing this error since two days.
Please help me to solve this problem.