从铬到C#获取URL不工作 [英] Get URL from chrome to C# not working
问题描述
- 我发现这个解决方案:
static void Main(string[] args)
{
foreach (Process process in Process.GetProcessesByName("chrome"))
{
string url = GetChromeUrl(process);
if (url == null)
continue;
Console.WriteLine("CH Url for '" + process.MainWindowTitle + "' is " + url);
}
}
public static string GetChromeUrl(Process process)
{
if (process == null)
throw new ArgumentNullException("process");
if (process.MainWindowHandle == IntPtr.Zero)
return null;
AutomationElement element = AutomationElement.FromHandle(process.MainWindowHandle);
if (element == null)
return null;
AutomationElement edit = element.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
return ((ValuePattern)edit.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
}
- 但不是工作在过去的铬版(32.0.1700.102米)。有人可以告诉更多通用的解决方案?
- 在Chrome浏览器可能已经改变了一些东西,上面行走需要进行修改。 :(
- 在此先感谢。
推荐答案
而不是使用此: element.FindAll(TreeScope.Subtree,新PropertyCondition(AutomationElement.ControlTypeProperty,ControlType.Edit)); 这code工作:
Use this instead: element.FindAll(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit)); this code works :
using System;
使用System.Collections.Generic; 使用System.Diagnostics程序; 使用System.Linq的; 使用System.Text; 使用System.Threading.Tasks; 使用System.Windows.Automation;
using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Automation;
命名空间测试 { 类节目 { 静态无效的主要(字串[] args) { 的foreach(工艺过程Process.GetProcessesByName(铬)) { 字符串URL = GetChromeUrl(过程); 如果(网址== NULL) 继续;
namespace tests { class Program { static void Main(string[] args) { foreach (Process process in Process.GetProcessesByName("chrome")) { string url = GetChromeUrl(process); if (url == null) continue;
Console.WriteLine("CH Url for '" + process.MainWindowTitle + "' is " + url);
}
}
public static string GetChromeUrl(Process process)
{
if (process == null)
throw new ArgumentNullException("process");
if (process.MainWindowHandle == IntPtr.Zero)
return null;
AutomationElement element = AutomationElement.FromHandle(process.MainWindowHandle);
if (element == null)
return null;
AutomationElementCollection edits5 = element.FindAll(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
AutomationElement edit = edits5[0];
string vp = ((ValuePattern)edit.GetCurrentPattern(ValuePattern.Pattern)).Current.Value as string;
Console.WriteLine(vp);
return vp;
}
}
}
这篇关于从铬到C#获取URL不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!