Accessing URL from Browsers like Google ,Firefox, Edge , Explorer,opera
#region Windows API Functions Declarations
//This Function is used to get Active Window Title...
[System.Runtime.InteropServices.DllImport("user32.dll",CharSet=System.Runtime.InteropServices.CharSet.Auto)]
public static extern int GetWindowText(IntPtr hwnd,string lpString, int cch);
//This Function is used to get Handle for Active Window...
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)]
private static extern IntPtr GetForegroundWindow();
//This Function is used to get Active process ID...
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)]
private static extern Int32 GetWindowThreadProcessId(IntPtr hWnd,out Int32 lpdwProcessId);
#endregion
public static Int32 GetWindowProcessID(IntPtr hwnd)
{
//This Function is used to get Active process ID...
Int32 pid;
GetWindowThreadProcessId(hwnd, out pid);
return pid;
}
public static IntPtr getforegroundWindow()
{
//This method is used to get Handle for Active Window using GetForegroundWindow() method present in user32.dll
return GetForegroundWindow();
}
public static string ActiveApplTitle()
{
//This method is used to get active application's title using GetWindowText() method present in user32.dll
IntPtr hwnd =getforegroundWindow();
if (hwnd.Equals(IntPtr.Zero)) return "";
string lpText = new string((char) 0, 5000);
int intLength = GetWindowText(hwnd, lpText, lpText.Length);
if ((intLength <= 0) || (intLength > lpText.Length)) return "System Application";
return lpText.Trim();
}
Process p = Process.GetProcessById(pid);
strProcessUrl = APIFuncs.GetProcessURL(p);
public static string GetProcessURL(Process process)
{
AutomationElement UrlBarElement = null;
AutomationElement mainWindowElement = null;
AutomationElement rootElement = null;
try
{
mainWindowElement = AutomationElement.FromHandle(process.MainWindowHandle);
if (mainWindowElement == null)
{
// log.Warn(logPrefix + " - Unable to capture the URL as MainWindowElement is Null.");
return null;
}
switch (process.ProcessName)
{
case "ApplicationFrameHost":
var elm = mainWindowElement.FindFirst(TreeScope.Children, new AndCondition(
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window),
new PropertyCondition(AutomationElement.NameProperty, "Microsoft Edge")));
UrlBarElement = elm.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
break;
case "opera":
UrlBarElement = mainWindowElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty,"opera" ));
break;
case "chrome":
//Condition chromeConditions = new AndCondition(
// new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit),
// new PropertyCondition(AutomationElement.NameProperty, "chrome", PropertyConditionFlags.IgnoreCase));
//UrlBarElement = mainWindowElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, ControlType.Edit));
AutomationElement elm1 = mainWindowElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Google Chrome"));
AutomationElement elm2 = TreeWalker.RawViewWalker.GetLastChild(elm1);
AutomationElement elm3 = elm2.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""));
AutomationElement elm4 = elm3.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ToolBar));
UrlBarElement = elm1.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
//var elm1 = mainWindowElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Google Chrome"));
//if (elm1 == null) { return null; } // not the right chrome.exe
//var elm2 = elm1.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""))[1];
//var elm3 = elm2.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""))[1];
//var elm4 = elm3.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""))[1];
//var elm5 = elm4.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""));
//UrlBarElement = elm5.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
break;
case "firefox":
UrlBarElement = mainWindowElement.FindFirst(TreeScope.Subtree,
new AndCondition(
new PropertyCondition(AutomationElement.NameProperty, "Search with Google or enter address"),
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)));
break;
default:
UrlBarElement = mainWindowElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
break;
}
if (UrlBarElement != null)
{
string urlbar = string.Empty;
if (!(bool)UrlBarElement.GetCurrentPropertyValue(AutomationElement.HasKeyboardFocusProperty))
{
string sURL = ((ValuePattern)UrlBarElement.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
string[] Url = sURL.Split('/');
if(Url[0].Trim().ToLower().Contains("https:"))
{
urlbar = sURL;
}
else
{
urlbar = "https://" + sURL;
}
}
var uri = new Uri(urlbar);
var baseUrl = uri.GetLeftPart(System.UriPartial.Authority);
return baseUrl;
//var urlbar = ((ValuePattern)UrlBarElement.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
}
else
{
return null;
}
}
catch (Exception exx)
{
}
finally
{
mainWindowElement = null;
process = null;
UrlBarElement = null;
rootElement = null;
//GC.Collect();
}
return null;
}
//This Function is used to get Active Window Title...
[System.Runtime.InteropServices.DllImport("user32.dll",CharSet=System.Runtime.InteropServices.CharSet.Auto)]
public static extern int GetWindowText(IntPtr hwnd,string lpString, int cch);
//This Function is used to get Handle for Active Window...
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)]
private static extern IntPtr GetForegroundWindow();
//This Function is used to get Active process ID...
[System.Runtime.InteropServices.DllImport("user32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto)]
private static extern Int32 GetWindowThreadProcessId(IntPtr hWnd,out Int32 lpdwProcessId);
#endregion
public static Int32 GetWindowProcessID(IntPtr hwnd)
{
//This Function is used to get Active process ID...
Int32 pid;
GetWindowThreadProcessId(hwnd, out pid);
return pid;
}
public static IntPtr getforegroundWindow()
{
//This method is used to get Handle for Active Window using GetForegroundWindow() method present in user32.dll
return GetForegroundWindow();
}
public static string ActiveApplTitle()
{
//This method is used to get active application's title using GetWindowText() method present in user32.dll
IntPtr hwnd =getforegroundWindow();
if (hwnd.Equals(IntPtr.Zero)) return "";
string lpText = new string((char) 0, 5000);
int intLength = GetWindowText(hwnd, lpText, lpText.Length);
if ((intLength <= 0) || (intLength > lpText.Length)) return "System Application";
return lpText.Trim();
}
Process p = Process.GetProcessById(pid);
strProcessUrl = APIFuncs.GetProcessURL(p);
public static string GetProcessURL(Process process)
{
AutomationElement UrlBarElement = null;
AutomationElement mainWindowElement = null;
AutomationElement rootElement = null;
try
{
mainWindowElement = AutomationElement.FromHandle(process.MainWindowHandle);
if (mainWindowElement == null)
{
// log.Warn(logPrefix + " - Unable to capture the URL as MainWindowElement is Null.");
return null;
}
switch (process.ProcessName)
{
case "ApplicationFrameHost":
var elm = mainWindowElement.FindFirst(TreeScope.Children, new AndCondition(
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window),
new PropertyCondition(AutomationElement.NameProperty, "Microsoft Edge")));
UrlBarElement = elm.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
break;
case "opera":
UrlBarElement = mainWindowElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty,"opera" ));
break;
case "chrome":
//Condition chromeConditions = new AndCondition(
// new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit),
// new PropertyCondition(AutomationElement.NameProperty, "chrome", PropertyConditionFlags.IgnoreCase));
//UrlBarElement = mainWindowElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, ControlType.Edit));
AutomationElement elm1 = mainWindowElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Google Chrome"));
AutomationElement elm2 = TreeWalker.RawViewWalker.GetLastChild(elm1);
AutomationElement elm3 = elm2.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""));
AutomationElement elm4 = elm3.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ToolBar));
UrlBarElement = elm1.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
//var elm1 = mainWindowElement.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Google Chrome"));
//if (elm1 == null) { return null; } // not the right chrome.exe
//var elm2 = elm1.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""))[1];
//var elm3 = elm2.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""))[1];
//var elm4 = elm3.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""))[1];
//var elm5 = elm4.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, ""));
//UrlBarElement = elm5.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
break;
case "firefox":
UrlBarElement = mainWindowElement.FindFirst(TreeScope.Subtree,
new AndCondition(
new PropertyCondition(AutomationElement.NameProperty, "Search with Google or enter address"),
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)));
break;
default:
UrlBarElement = mainWindowElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
break;
}
if (UrlBarElement != null)
{
string urlbar = string.Empty;
if (!(bool)UrlBarElement.GetCurrentPropertyValue(AutomationElement.HasKeyboardFocusProperty))
{
string sURL = ((ValuePattern)UrlBarElement.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
string[] Url = sURL.Split('/');
if(Url[0].Trim().ToLower().Contains("https:"))
{
urlbar = sURL;
}
else
{
urlbar = "https://" + sURL;
}
}
var uri = new Uri(urlbar);
var baseUrl = uri.GetLeftPart(System.UriPartial.Authority);
return baseUrl;
//var urlbar = ((ValuePattern)UrlBarElement.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
}
else
{
return null;
}
}
catch (Exception exx)
{
}
finally
{
mainWindowElement = null;
process = null;
UrlBarElement = null;
rootElement = null;
//GC.Collect();
}
return null;
}
Comments
Post a Comment