我正在尝试从Opera浏览器获取URL,我认为它与Chrome相同,但我错了,因为它不能以这种方式工作。
到目前为止我所做的一切是: public static string GetURL(IntPtr intPtr, string programName, out string url)
{
string temp = null;
if (programName.Equals("opera"))
{
// // there are always multiple opera processes, so we have to loop through all of them to find the
// // process with a Window Handle and an automation element of name "Address and search bar"
/* string x = "";
DdeClient dde = new DdeClient("opera", "WWW_GetWindowInfo");
try
{
//temp := RequestData('0xFFFFFFFF');
dde.Connect();
string url1 = dde.Request("URL", int.MaxValue);
string[] text = url1.Split(new string[] { "\",\"" }, StringSplitOptions.RemoveEmptyEntries);
dde.Disconnect();
}
catch (Exception)
{
x = "failed";
}
*/
Process[] procsOpera = Process.GetProcessesByName("opera");
foreach (Process opera in procsOpera)
{
// the chrome process must have a window
if (opera.MainWindowHandle == IntPtr.Zero)
{
continue;
}
// find the automation element
AutomationElement elm = AutomationElement.FromHandle(opera.MainWindowHandle);
AutomationElement elmUrlBar = elm.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
// if it can be found, get the value from the URL bar
if (elmUrlBar != null)
{
AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns();
if (patterns.Length > 0)
{
ValuePattern val = (ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0]);
temp = val.Current.Value.ToString();
url = val.Current.Value.ToString();
}
else
{
temp = "";
url = "";
}
}
else
{
temp = "";
url = "";
}
}
}
url = temp;
return temp;
}
我已经尝试过NDDE客户端和自动化元素,但两者都失败了:( 我认为在自动化元素中,问题出在这一行
AutomationElement elmUrlBar = elm.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
也许对于歌剧来说,不是"Address and search bar"
可以帮助我解决这个问题吗?
注意:有几个问题,其中chrome和opera标记了但是SO内部没有Opera工作答案。
答案 0 :(得分:1)
您可以使用提供与UISpy相同功能的Inspect从Opera UI获取AutomationId。我认为它不起作用的原因是因为它在Opera中的名称不同。
答案 1 :(得分:1)
您可以下载NDde dll
。添加引用后,只需将此代码复制到您想要获取URL的位置。
NDde DLL下载:http://ndde.codeplex.com/
DdeClient dde = new DdeClient("opera", "WWW_GetWindowInfo");
dde.Connect();
string url = dde.Request("URL", int.MaxValue);
string[] text = url.Split(new string[] { "\",\"" }, StringSplitOptions.RemoveEmptyEntries);
dde.Disconnect();
如果您想获取firefox数据,只需将opera
更改为firefox
DdeClient dde = new DdeClient("firefox", "WWW_GetWindowInfo");
dde.Connect();
string url = dde.Request("URL", int.MaxValue);
string[] text = url.Split(new string[] { "\",\"" }, StringSplitOptions.RemoveEmptyEntries);
dde.Disconnect();
在函数中
private string GetBrowserURL(string browser) {
try {
DdeClient dde = new DdeClient(browser, "WWW_GetWindowInfo");
dde.Connect();
string url = dde.Request("URL", int.MaxValue);
string[] text = url.Split(new string[] { "\",\"" }, StringSplitOptions.RemoveEmptyEntries);
dde.Disconnect();
return text[0].Substring(1);
} catch {
return null;
}
}
答案 2 :(得分:1)
获取opera的进程ID:
public static void GetOperaURL() { Process[] OProcesses = Process.GetProcessesByName("opera"); string url = null; foreach (Process proc in OProcesses) { if (proc.MainWindowHandle != IntPtr.Zero) { url = getOperaUrlFromProcess(proc); break; } } }
使用自动化元素确定网址
private static string getOperaUrlFromProcess(Process proc)
{
// find the automation element
AutomationElement elm = AutomationElement.FromHandle(proc.MainWindowHandle);
// manually walk through the tree, searching using TreeScope.Descendants is too slow (even if it's more reliable)
AutomationElement elmUrlBar = null;
try
{
var elm1 = elm.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, "Browser container"));
if (elm1 == null) { return null; }
elmUrlBar = elm.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Address field"));
}
catch
{
// Chrome has probably changed something, and above walking needs to be modified. :(
// put an assertion here or something to make sure you don't miss it
return null;
}
// make sure it's valid
if (elmUrlBar == null)
{
// it's not..
return null;
}
// there might not be a valid pattern to use, so we have to make sure we have one
AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns();
if (patterns.Length == 1)
{
string ret = "";
try
{
ret = ((ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0])).Current.Value;
}
catch { }
if (ret != "")
{
return ret;
}
return null;
}
return null;
}
答案 3 :(得分:1)
更改了您的功能并使其尽可能简单。适用于当前Opera版本47.0
public static string GetOperaURL()
{
string url = "";
Process[] procsOpera = Process.GetProcessesByName("opera");
foreach (Process opera in procsOpera)
{
// the chrome process must have a window
if (opera.MainWindowHandle == IntPtr.Zero)
{
continue;
}
// find the automation element
AutomationElement elm = AutomationElement.FromHandle(opera.MainWindowHandle);
AutomationElement elmUrlBar = elm.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.NameProperty, "Address field"));
// if it can be found, get the value from the URL bar
if (elmUrlBar == null) continue;
AutomationPattern pattern = elmUrlBar.GetSupportedPatterns().FirstOrDefault(wr=>wr.ProgrammaticName == "ValuePatternIdentifiers.Pattern");
if (pattern == null) continue;
ValuePattern val = (ValuePattern)elmUrlBar.GetCurrentPattern(pattern);
url = val.Current.Value;
break;
}
return url;
}