C# 探测并捕获Javascript Alert() 和Confirm()
所以我已经做了一些研究一段时间了,但我陷入了死胡同。我正在做一些 IE 自动化。在 C#/.NET 中,如何探测并使用 javascriptalert() 或confirm(),以便我可以执行诸如抓取其文本并单击“确定\取消”按钮之类的操作?
更新:
我需要重申:我需要能够从alert()或confirm()中提取和验证文本,并向其发送“确定”或“取消”点击。此类测试的一个示例是确保当我单击删除时,confirm() 不会显示“您确定要去墨西哥吗?”或其他内容除了正确的消息之外的其他内容。
为了以防万一,让我重申一下:出于本次测试的目的,我对相关网站的来源具有零控制权。
最后,我使用的是 SHDocVw.InternetExplorer
。
So I've been doing some research for a while and I'm at a dead end. I'm doing some IE automation. In C#/.NET, how do I probe for and consume a javascript alert() or confirm() so I can do things like grab its text and click the OK\Cancel buttons?
Update:
I need to reiterate: I need to be able to pull and verify the text from an alert() or confirm() as well as send it an OK or Cancel click. An example of such a test would be to make sure that, when I click on delete, the confirm() doesn't say "Are you sure you'd like to go to Mexico?" or anything else except the proper message.
Just in case, let me reiterate: For the purposes of this test, I have zero control over the source of the website in question.
Lastly, I'm using SHDocVw.InternetExplorer
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您将不得不使用 FindWindow、SendMessage 等。这就是 WatiN 进行对话框处理的方式。
I think you're going to have to use FindWindow, SendMessage, etc. This is how WatiN does its dialog handling.
我认为您正在寻找的是 IDocHostshowUI< /a> COM 接口。如果我正确阅读了文档并且我的假设是正确的 当在JavaScript中调用alert()或confirm()时,ShowMessage函数将被调用。
我自己没有这样做过,所以我只是猜测。以下是一些示例,其中有人展示了在C#。 WebBrowserSample2 听起来像是您正在寻找的。
I think what you are looking for is the IDocHostshowUI COM interface. If I read the documentation correctly and my assumptions are correct ShowMessage function will be called when an alert() or confirm() is called in JavaScript.
I've not done this myself so I am only guessing. Here are some examples of someone showing an example of using that interface (and many others) in C#. WebBrowserSample2 sounds like what you are looking for.
FindWindow - 将让您接近;你确实想要一个窗把手,但你不确定如何找到它。
最好使用 EnumWindows。这是一个例程的示例,您可以每隔一两秒运行一次,它将为您提供有关系统上每个窗口的大量信息,然后您可以对其进行分析。您可以检查窗口的类型、其中的文本、获取按钮并向右侧发送一条单击消息。它会让你做你希望能够做的一切。
要获取文本,您可以发送 wm_gettext,对于按钮,您可以使用 wm_click。
对 C 感到抱歉。我能找到的下一个最好的例子是 LISP。花点时间阅读代码,你就会明白这个想法是什么。匹配函数有助于在不确切知道文本的情况下确定文本是否正确。
如果你能设法在 c# 中做到这一点,我打赌你会得到更短的解决方案。 (您可以在 EnumWindows 的输出上使用 LINQ 吗?;-)
FindWindow - will get you close; you do want a window handle but you won't for sure know how to find it.
Better would be do use EnumWindows.Here's an example of a routine you can run every second or two that will give you a ton of info about every single window on the system, then you can analyze it. You can check the type of window, the text in it, get the buttons, and send a click message to the right one. It will let you do everything you are hoping to be able to do.
To get the text you send a wm_gettext and for the button you may get away with wm_click.
Sorry for the C. the next best example I could find was in LISP. Take time to read the code, you will see what the idea is. The Matching functions help determine if the text is right without knowing it exactly.
If you can manage to do this in c# I am betting you will get a lot shorter solution. (can you use LINQ on the output of EnumWindows?;-)