首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用UI自动化从边缘浏览器检索文本

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

Stack Overflow用户
提问于 2015-09-17 16:39:09
回答 1查看 2.8K关注 0票数 7

这似乎过去起作用了,但现在不再起作用了。也许在某个地方有一些切换可以使它成为可能?使用此代码

代码语言:javascript
复制
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的解决方案是可以接受的,如果它可以做同样的事情。

EN

回答 1

Stack Overflow用户

发布于 2015-12-23 12:11:24

在编写Microsoft不受CodedUI支持的时候,他们已经提到他们正在评估支持,但是目前您不能使用它:这个链接显示了这个问题上的一个已归档的错误:https://connect.microsoft.com/VisualStudio/feedback/details/1551238/vs2015-supports-codedui-automation-test-for-edge-browser

WebDriver是目前实现Microsoft自动化的最佳方法。但是,看看上面的代码,您就不能做完全相同的事情了。使用WebDriver,您可以通过Id、ClassName、TagName、Name、LinkText、Partial、CSS、Xpath来定位一个元素,但据我所知,您无法像上面示例中所做的那样从x,y协调中找到一个对象。

才能开始使用Webdriver。创建一个控制台应用程序。安装下列软件包:

代码语言:javascript
复制
Install-Package Selenium.RC
Install-Package Selenium.WebDriver
Install-Package Selenium.WebDriverBackedSelenium
Install-Package Selenium.Support

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

有关Microsoft WebDriver的更多信息可以找到这里

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

代码语言:javascript
复制
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();
                }
            }
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32635604

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档