C# 从Opera浏览器获取URL
我试图从Opera浏览器中获取URL,我假设它与Chrome浏览器相同,但我错了,因为它不能以这种方式工作 到目前为止,我所做的是:C# 从Opera浏览器获取URL,c#,opera,C#,Opera,我试图从Opera浏览器中获取URL,我假设它与Chrome浏览器相同,但我错了,因为它不能以这种方式工作 到目前为止,我所做的是: public static string GetURL(IntPtr intPtr, string programName, out string url) { string temp = null; if (programName.Equals("opera")) { //
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"));
也许对于opera来说,它不是“地址和搜索栏”,有人能帮我解决这个问题吗
注意:chrome和opera标记了两个问题,但里面没有opera工作答案。您可以使用它提供与UISpy相同的功能从opera UI获取AutomationId。我认为它不工作的原因是因为它在opera中的命名不同。您可以使用哪个提供与UISpy相同的功能,从Opera UI获取AutomationId。我认为它不起作用的原因是因为它在Opera中的名称不同。您可以下载
NDde dll
。添加引用后,只需将此代码复制到您想要获取url的位置即可
NDde DLL下载:
如果您想获取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;
}
}
您可以下载
NDde dll
。添加引用后,只需将此代码复制到您想要获取url的位置即可
NDde DLL下载:
如果您想获取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;
}
}
更改了您的功能并使其尽可能简单。适用于当前的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;
}
更改了您的功能并使其尽可能简单。适用于当前的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;
}
非常感谢!我正在寻找的元素名为“
地址字段”
,如果将来有人需要它。非常感谢!我正在寻找的元素名为“地址字段”
,如果将来有人需要它。我已经试过了(在我的问题中有注释)但是它对Opera不起作用,但是我在firefox和IE上使用它。无论如何,谢谢你的回答!我已经尝试过了(在我的问题中有评论),但是它对Opera不起作用,但是我在firefox和IE上使用它。无论如何,谢谢你的回答!