如何获取Chrome和Firefox打开的网页的url?我正在编写系统托盘应用程序,需要检查基于内部网络的应用程序是否打开。我可以使用以下方法检查IE:SHDocVw.ShellWindowsshellWindows=newSHDocVw.ShellWindows();字符串文件名;布尔sdOpen=假;foreach(SHDocVw.InternetExplorerieinshellWindows){filename=Path.GetFileNameWithoutExtension(ie.FullName).ToLower();if(filename.Equals("iexplore")){string[]urlParts=(ie.LocationURL.ToString()).Split('/');字符串网站=urlParts[2];如果(网站==“myApp:8080”){sdOpen=true;};}}if(sdOpen){Console.WriteLine("Appisopen");}else{Console.WriteLine("应用未打开");};安慰。读键(真);然而,该系统的一些用户更喜欢使用Chrome或Firefox。我怎样才能为Chrome和Firefox执行与上面相同的操作(即获取浏览器中任何打开的选项卡的url)?(我不会为其他浏览器费心,因为我们组织中只使用这些浏览器。)它特定于每个浏览器。这是主要的:编辑:Chrome自2014年以来发生了变化,您需要获取具有辅助功能的URL。使用DDE(使用NDDE–.NET唯一优秀的DDE打包器)从Firefox/Opera获取URL的代码:////usage:GetBrowserURL("opera")orGetBrowserURL("firefox")//privatestringGetBrowserURL(string浏览器){尝试{DdeClientdde??=newDdeClient(浏览器,“WWW_GetWindowInfo”);dde.Connect();stringurl=dde.Request("URL",int.MaxValue);string[]text=url.Split(newstring[]{"",""},StringSplitOptions.RemoveEmptyEntries);dde.Disconnect();返回文本[0].子字符串(1);}赶上{返回空;}}使用UIAutomation–获取FireFox和Chrome的url:elseif(browser==BrowserType.Chrome){//"Chrome_WidgetWin_1"Process[]procsChrome=Process.GetProcessesByName("chrome");foreach(ProcesschromeinprocsChrome){//chrome进程必须有一个窗口if(chrome.MainWindowHandle==IntPtr.Zero){continue;}//AutomationElementelm=AutomationElement.RootElement.FindFirst(TreeScope.Children,//newPropertyCondition(AutomationElement.ClassNameProperty,"Chrome_WidgetWin_1"));//找到自动化元素AutomationElementelm=AutomationElement.FromHandle(chrome.MainWindowHandle);//手动遍历树,使用TreeScope.Descendants搜索太慢(即使它更可靠)AutomationElementelmUrlBar=null;try{//使用inspect.exe(WindowsSDK)forChrome29.0.1547.76m(当前最新的稳定版本)找到的步行路径varelm1=elm.FindFirst(TreeScope.Children,newPropertyCondition(AutomationElement.NameProperty,"GoogleChrome"));varelm2=TreeWalker.ControlViewWalker.GetLastChild(elm1);//我不知道用于查找的条件:(varelm3=elm2.FindFirst(TreeScope.Children,newPropertyCondition(AutomationElement.NameProperty,""));varelm4=elm3.FindFirst(TreeScope.Children,newPropertyCondition(AutomationElement.ControlTypeProperty,ControlType.ToolBar));elmUrlBar=elm4.FindFirst(TreeScope.Children,newPropertyCondition(AutomationElement.NameProperty,"地址和searchbar"));}catch{//Chrome可能已经改变了一些东西,上面的行走需要修改。:(//在这里放一个断言或其他东西以确保你不会错过它continue;}//makesureit'svalidif(elmUrlBar==null){//it'snot..continue;}//elmUrlBar现在是URL栏元素。如果我们想获得有效的URL,我们必须确保它不在键盘焦点之外,如果((bool)elmUrlBar.GetCurrentPropertyValue(AutomationElement.HasKeyboardFocusProperty)){continue;}//可能没有可用的有效模式,所以我们必须确保我们有一个AutomationPattern[]patterns=elmUrlBar.GetSupportedPatterns();如果(patterns.Length==1){stringret="";try{ret=((ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0])).Current.Value;}catch{}if(ret!=""){//必须匹配域名(前面可能有“https://”)if(Regex.IsMatch(ret,@"^(https://)?[a-zA-Z0-9-.]+(.[a-zA-Z]{2,4}).*$")){//将http://添加到theurl,因为如果它不是SSL,Chrome会隐藏它if(!ret.StartsWith("http")){ret="http://"+ret;返回ret;}}继续;}}}elseif(browser==BrowserType.Firefox){AutomationElementroot=AutomationElement.RootElement.FindFirst(TreeScope.Children,newPropertyCondition(AutomationElement.ClassNameProperty,"MozillaWindowClass"));ConditiontoolBar=newAndCondition(newPropertyCondition(AutomationElement.ControlTypeProperty,ControlType.ToolBar),newPropertyCondition(AutomationElement.NameProperty,"浏览器选项卡"));vartool=root.FindFirst(TreeScope.Children,toolBar);vartool2=TreeWalker.ControlViewWalker.GetNextSibling(工具);varchildren=tool2.FindAll(TreeScope.Children,Condition.TrueCondition);foreach(AutomationElementiteminchildren){foreach(AutomationElementiinitem.FindAll(TreeScope.Children,Condition.TrueCondition)){foreach(AutomationElementiiini.FindAll(TreeScope.Children,Condition.TrueCondition)){if(ii.Current.LocalizedControlType=="document"){if(!ii.Current.BoundingRectangle.X.ToString().Contains("Infinity")){ValuePatternactiveTab=ii.GetCurrentPattern(ValuePattern.Pattern)作为值模式;varactiveUrl=activeTab.Current.Value;返回活动网址;}}}}}}也许这段代码可以提供帮助;感谢BLEZ分享此代码我使用此代码从firefox捕获唯一地址并将其添加到列表框中。但我认为这不适合Chrome?(您应该将NDde.dll添加到您的项目中,为此请转到解决方案资源管理器,右键单击引用->添加引用->浏览->从二进制文件文件夹中找到DLL(http://ndde.codeplex.com/.))使用系统;使用System.Collections.Generic;使用System.ComponentModel;使用系统数据;使用系统绘图;使用System.Linq;使用系统文本;使用System.Windows.Forms;使用NDde.Client;namespaceWindowsFormsApplication9{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbutton1_Click(objectsender,EventArgse){timer1.Enabled=true;}privatestringGetBrowserURL(stringbrowser){try{DdeClientdde??=newDdeClient(browser,"WWW_GetWindowInfo");dde.Connect();stringurl=dde.Request("URL",int.MaxValue);string[]text=url.Split(newstring[]{"",""},StringSplitOptions.RemoveEmptyEntries);dde.Disconnect();返回文本[0].子字符串(1);}赶上{返回空;}}privatevoidtimer1_Tick(objectsender,EventArgse){intj=0;for(inti=0;i以下代码适用于Chrome版58.0.3029.110:请在.NET提供的Assembly中添加UIAutomationClient和UIAutomationProvider的参考。以上就是C#学习教程:如何获取Chrome和Firefox打开的网页的url?分享的所有内容,如果对你有用,需要了解更多C#学习教程,希望大家多多关注---foreach(ProcessprocinprocsChrome){//chrome进程必须有窗口if(proc.MainWindowHandle==IntPtr.Zero)继续;//要找到选项卡,我们首先需要找到可靠的东西-“新选项卡”按钮AutomationElementroot=AutomationElement.FromHandle(proc.MainWindowHandle);varSearchBar=root.FindFirst(TreeScope.Descendants,newPropertyCondition(AutomationElement.NameProperty,"地址和搜索栏"));如果(SearchBar!=null)返回(字符串)SearchBar.GetCurrentPropertyValue(ValuePatternIdentifiers.ValueProperty);}本文收集自网络,不代表立场。如涉及侵权请点击右侧联系管理员删除。如需转载请注明出处:
