Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用AutomationElement从Chrome获取URL_C#_Ui Automation - Fatal编程技术网

C# 使用AutomationElement从Chrome获取URL

C# 使用AutomationElement从Chrome获取URL,c#,ui-automation,C#,Ui Automation,我有一个监控浏览器URL的应用程序。目前我正在使用chrome。我遵循以下解决方案: 然后我使用此方法将URL记录到文本文件中(使用log4net): 它工作得很好。但不知何故,与GetChromeUrl方法存在不一致。有时它返回null,这是我的大问题。有谁有更好的解决方案吗?哪个位返回null?Windows通常会忽略ShowInTaskbar属性已更改的程序。这可能是第一次检查(和第二次检查)中出现null的原因。关于第二个错误,我无法提供任何帮助,但几周前我遇到了同样的问题。我决定使用C

我有一个监控浏览器URL的应用程序。目前我正在使用chrome。我遵循以下解决方案:

然后我使用此方法将URL记录到文本文件中(使用log4net):


它工作得很好。但不知何故,与
GetChromeUrl
方法存在不一致。有时它返回
null
,这是我的大问题。有谁有更好的解决方案吗?

哪个位返回null?Windows通常会忽略ShowInTaskbar属性已更改的程序。这可能是第一次检查(和第二次检查)中出现null的原因。关于第二个错误,我无法提供任何帮助,但几周前我遇到了同样的问题。我决定使用Chrome的URL栏的position元素,将光标位置更改为它,然后在光标下获取AutomationElement:

    Form1_Load()
    {
        Cursor.Position = element's position;
        Timer.Start();
    }

    Timer_Tick()
    {
        AutomationElement element = AutomationElement.FromPoint(new System.Windows.Point(mouse.X, mouse.Y));
            string current = element.Current.Name;
        Console.WriteLine(current); //log in text file...
    }

这是否为您的查询提供了答案?

哪一行返回null?主窗口句柄检查?FromHandle检查或当前的.Value?嗨,Simon,这行返回空的AutomationElement edit=element.FindFirst(TreeScope.Children,newPropertyCondition(AutomationElement.ControlTypeProperty,ControlType.edit));我还注意到,当你右键点击chrome中的页面,然后什么也不做,它也会返回一个空值。谢谢,我找到了另一种抛出空值的方法。如果将鼠标悬停在链接栏或标题栏上,则带有长文本的标题栏(如果它弹出整个文本或作为工具提示弹出)将抛出空值。看来这会给我的工作带来麻烦。嗯。。我这里没有魔弹。当你想“编程”一个不是专门为此而设计的远程应用程序时,你只需要构建积木(ui自动化),你需要像Patrick的回答中那样的技巧来管理你的出路。感谢回复,我已经创建了一个解决方案,将上一个链接存储在一个变量中,如果用户强制它抛出空值(通过在页面上单击鼠标右键,在链接或标题栏上单击鼠标等方式),因为代码将抛出异常,我刚刚捕获它并检索上一个链接的值。我正在尝试你的代码,但我无法获得鼠标。x和y?这是鼠标的值还是url栏的值?当我们将光标设置在它的位置时,鼠标和url栏。一旦你有了automationElement,你可能会得到一个id并不断地得到它的名字,而不必让鼠标停在那里。我想写,但我在打电话。至少不值得向上投票吗?@Fadz您可能可以创建新的点(Cursor.Position),但我不确定。@Fadz这里有一些链接,可以获取URL栏的id:和。试试那个方法吧!
while (true) {
    foreach (Process process in Process.GetProcessesByName("chrome")) {
        string url = GetChromeUrl(process);
        if (url == null)
            continue;
        Console.WriteLine(url); //for viewing purpose, it actually logs in orig code
    }
    Thread.Sleep(1000);
}
    Form1_Load()
    {
        Cursor.Position = element's position;
        Timer.Start();
    }

    Timer_Tick()
    {
        AutomationElement element = AutomationElement.FromPoint(new System.Windows.Point(mouse.X, mouse.Y));
            string current = element.Current.Name;
        Console.WriteLine(current); //log in text file...
    }