我如何使用UI自动化检索文本从边缘浏览器

本文关键字:文本 边缘 浏览器 检索 自动化 何使用 UI | 更新日期: 2023-09-27 18:13:22

这似乎过去是有效的,但现在不再有效了。也许在某个地方有一些开关可以启用它?使用以下代码

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的解决方案是可以接受的,如果它能做同样的事情。

我如何使用UI自动化检索文本从边缘浏览器

在撰写本文时,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 LinkText, CSS, Xpath来定位元素,但据我所知,您不能像在上面的例子中那样从x, y坐标定位对象。

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

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

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

    对于Windows 10 Build 10240,请安装此版本的Microsoft WebDriver。
  • Windows 10秋季2015更新,安装微软WebDriver秋季2015更新。
  • 对于Windows内部程序的最新预览版本,请安装此版本的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();
                }
            }
        }
    }
}