gpt4 book ai didi

c# - 如何使用 UI 自动化从 Edge 浏览器检索文本

转载 作者:太空狗 更新时间:2023-10-29 22:00:30 26 4
gpt4 key购买 nike

这似乎曾经有效,但现在无效了。也许某处有一些开关可以启用它?使用此代码

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 应用程序(但如果您滚动得太快,它会有点不稳定)。对于 list ,我使用的是 uiAccess=true,AsInvoker。当以管理员身份运行时,它没有帮助。

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

最佳答案

在撰写本文时,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、部分链接文本、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();
}
}
}
}
}

关于c# - 如何使用 UI 自动化从 Edge 浏览器检索文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32635604/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com