C# 中的 SendKeys 类

发布于 2024-07-26 21:04:22 字数 470 浏览 1 评论 0原文

我有一个 c# 程序,它使用 Process 类来启动 Internet Explorer 并转到某个 url。

然后,它将特定字符串发送到该页面的搜索框,复制返回结果的整个屏幕,并对其进行处理。

我遇到的问题是这样的:

我使用 SendKeys.SendWait("abc") 将字符串发送到 Internet Explorer 中的页面(活动窗口)。 当程序运行时,我看到页面上填充的内容有时是“bc”,有时是“abbc”,有时是“abcc”,有时是正确的“abc”。 每次跑步看起来都完全不同。 我测试的多台机器上都出现了这个问题。 但在我自己的机器上,也就是我最初开发这个的地方,我从未见过它发生——只有当我在其他机器上测试它时(所有机器都运行 XP)。

我在 SendWait 语句之间添加了延迟,以解决随机计时问题,但我是否相信即使在相同的 SendWait 语句中也存在问题?

需要帮助。 谢谢。

I have a c# program that uses the Process class to launch Internet Explorer and goes to a url.

It then sends in specific strings to the search box of that page, copies the whole screen of the returned results, and processes it.

The problem I encountered is this:

I use SendKeys.SendWait("abc") to send the string to the page in Internet Explorer (the active window). As the program is running, I see that what is being populated on the page is sometimes "bc", sometimes "abbc", sometimes "abcc", and sometimes correctly "abc". Each run looks totally different. This problem happened on multiple machines I tested. But on my own machine, where I originally developed this, I've never seen it happen - only when I test it on other machines (all running XP).

I put in delays in between SendWait statements to take care of random timing issues, but am I to believe that even within the same SendWait statements there are issues?

Help needed. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过期以后 2024-08-02 21:04:23

如果您尝试获取网页的源代码,那么我建议您使用类似的内容

WebClient client = new WebClient();
client.DownloadString("http://mypage");

如果您尝试获取页面上呈现的文本,那么我建议您查看 HtmlAgilityPack,它应该可以帮助您更轻松地获取页面内容并使其更加具体。
或者,您可以使用正则表达式来选择 div 标签之间的文本:

RegEx textSelector = new RegEx("<div>([^<]+?)</div>");
string pageText = "";
foreach(var match in textSelector.Matches(myHtml))
    pageText += match.Groups[0].Value;

警告,该代码是我凭空想象出来的,未经测试,并且可能会导致一些带有嵌套标签的相当随机的结果:)

If you are attempting to grab source of a web page, then I'd suggest using something like

WebClient client = new WebClient();
client.DownloadString("http://mypage");

If you are attempting to grab the text that gets rendered on the page, then I'd recommend looking at the HtmlAgilityPack which should allow you to much more easily grab the content of the page as well as making it more specific.
Alternatively, you could make use of a RegEx to select the text between, say, div tags:

RegEx textSelector = new RegEx("<div>([^<]+?)</div>");
string pageText = "";
foreach(var match in textSelector.Matches(myHtml))
    pageText += match.Groups[0].Value;

Warning, that code was written off the top of my head, untested, and will probably result in some pretty random results with nested tags :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文