繁体   English   中英

如何使用UI自动化从Edge Browser检索文本

[英]How do I use UI Automation to retrieve text from Edge Browser

似乎这曾经起作用,但不再适用。 也许在某个地方有一些切换可以实现它? 使用此代码

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的解决方案。

在撰写本文时,CodedUI不支持Microsoft Edge,他们提到他们正在评估支持,但目前您无法使用它:此链接显示了该问题的归档错误: https//connect.microsoft.com/VisualStudio /反馈/信息/ 1551238 / vs2015 -载体- codedui自动化测试换边缘浏览器

WebDriver是目前实现Microsoft Edge自动化的最佳方式。 但是看看上面的代码,你无法做同样的事情。 使用WebDriver,您可以通过Id,ClassName,TagName,Name,LinkText,Partial Link Text,CSS,Xpath查找元素,但据我所知,您无法像x,y坐标那样找到对象。上面的例子。

开始使用Webdriver。 创建一个控制台应用。 安装以下包:

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

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

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

然后,您可以添加一些代码来驱动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();
                }
            }
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM