【问题标题】:How do I use UI Automation to retrieve text from Edge Browser如何使用 UI 自动化从边缘浏览器中检索文本
【发布时间】:2015-12-14 15:13:08
【问题描述】:

这似乎曾经有效,但现在不再有效。也许某处有一些切换可以启用它?使用此代码

private static async Task<string> getText(double x, double y)
{
    try
    {
        var location = new System.Windows.Point(x, y);
        AutomationElement element = AutomationElement.FromPoint(location);

        object patternObj;
        if (element.TryGetCurrentPattern(TextPattern.Pattern, out patternObj))
        {
            var textPattern = (TextPattern)patternObj;

            var range = textPattern.RangeFromPoint(location);
            range.ExpandToEnclosingUnit(TextUnit.Word);

            var text = range.GetText(-1).Trim();
            return text;
        }
        else
        {
            return "no text found";
        }
    }
    catch (Exception ex)
    {
        return ex.Message;
    }
}

它确实适用于带有浏览器的 Metro 应用程序(但如果滚动太快会有点不稳定)。对于清单,我使用的是 uiAccess=true,AsInvoker。当以管理员身份运行时,它没有帮助。

更新。使用 WebDriver 的解决方案如果可以做同样的事情是可以接受的。

【问题讨论】:

  • 代码在什么时候不再按预期工作? (例如,不支持 Text 模式,或者 RangeFromPoint() 返回意外的范围。) Edge 中应该有一个支持 Text 模式的元素。所以也许 FromPoint() 没有返回那个元素。您能否检查该元素的属性以确定您拥有的元素。如果不是支持 Text 模式的元素,也许您可​​以从 FromPoint() 返回的元素导航到 Text 模式元素。
  • 虽然我需要,但从点开始可能不起作用。
  • 您的问题太宽泛了,因为它可以/应该在一般情况下起作用。请提供完整的复制案例。
  • 您能告诉我们您要输出什么文本吗? url,html,标题栏?请参阅此问题以使用 Windows 自动化进行 url 抓取:stackoverflow.com/a/32219999/1155847

标签: c# automation microsoft-edge microsoft-ui-automation


【解决方案1】:

在撰写本文时 CodedUI 不支持 Microsoft Edge,他们提到他们正在评估支持,但目前您无法使用它:此链接显示有关该问题的已提交错误:https://connect.microsoft.com/VisualStudio/feedback/details/1551238/vs2015-supports-codedui-automation-test-for-edge-browser

WebDriver 是目前实现 Microsoft Edge 自动化的最佳方式。但是查看上面的代码,您无法做完全相同的事情。使用 WebDriver,您可以通过 Id、ClassName、TagName、Name、LinkText、Partial Link Text、CSS、Xpath 来定位元素,但据我所知,您无法像在上面的例子。

开始使用 Webdriver。创建控制台应用程序。安装以下软件包:

Install-Package Selenium.RC
Install-Package Selenium.WebDriver
Install-Package Selenium.WebDriverBackedSelenium
Install-Package Selenium.Support

根据您的操作系统安装正确的 Microsoft WebDriver:

有关 Microsoft WebDriver 的更多信息,请访问here

然后您可以添加一些代码来驱动 WebDriver,以下示例转到我的博客并通过它的 css 类名获取一个元素:

using System;
using System.IO;
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;

namespace SampleGetText
{
    public class Program
    {
        static void Main(string[] args)
        {
            var text = GetText();
        }

        public static string GetText()
        {
            RemoteWebDriver driver = null;
            string serverPath = "Microsoft Web Driver";
            // Makes sure we uses the correct ProgramFiles depending on Enviroment
            string programfiles = Environment.Is64BitOperatingSystem ? "%ProgramFiles(x86)%" : "%ProgramFiles%";

            try
            {
                // Gets loaction for MicrosoftWebDriver.exe
                serverPath = Path.Combine(System.Environment.ExpandEnvironmentVariables(programfiles), serverPath);

                //Create a new EdgeDriver using the serverPath
                EdgeOptions options = new EdgeOptions();
                options.PageLoadStrategy = EdgePageLoadStrategy.Eager;
                driver = new EdgeDriver(serverPath, options);

                //Set a page load timeout for 5 seconds
                driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(5));

                // Navigate to my blog
                driver.Url = "https://blogs.msdn.microsoft.com/thebeebs/";

                // Find the first element on my screen with CSS class entry-title and return the text
                IWebElement myBlogPost = driver.FindElement(By.ClassName("entry-title"));
                return myBlogPost.Text;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return "";
            }
            finally
            {
                if (driver != null)
                {
                    driver.Close();
                }
            }
        }
    }
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2021-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-20
  • 2021-09-08
  • 2021-12-31
相关资源
最近更新 更多