【发布时间】:2014-03-24 23:01:51
【问题描述】:
我正在尝试从 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" 可以请1 帮我解决这个问题吗?
注意:有几个问题在哪里标记了 chrome 和 opera,但在 SO 上没有 Opera 工作答案。
【问题讨论】: