如何让 C# WebDriver Windows 在 IE 和新窗口中使用 SwitchTo

发布于 2024-12-08 16:38:57 字数 578 浏览 0 评论 0原文

我对 C# 编程还是个新手,并且已经能够让 WebDriver 正常工作来完成我需要在我们的网站上完成的大部分工作,除非启动新窗口。似乎 Java 中有很多选项可以处理这个问题,但我似乎无法在如何收集窗口句柄然后切换到新生成的窗口句柄方面跳到 C#。我面临的部分问题是,由于新窗口需要几秒钟的时间才能填充,所以我正在尝试等待才能捕获新窗口。当我最初这样做时,我尝试使用它

wait.Until(WebDriver => driver.Title.Equals("_blank"));
driver.SwitchTo().Window("_blank") 

来确保在尝试切换到窗口之前已生成窗口。

然后阅读更多内容,我发现我必须在进行切换之前捕获 windowHandles,并且由于我仍然需要等待窗口生成并填充,我想我需要一些时间来检查新句柄并切换到它,那时我不知道该怎么办。

我正在使用 selenium2 的 .Net WebDriver 和 SpecFlow,因此所有测试都是用 C# 进行的,虽然我没有用 C# 做过太多高级工作,但我感觉 C# 比 Java 更舒服。如果有人有一些提示可以帮助我开始解决这个问题,我将不胜感激。

I'm still new to C# programming and have been able to get WebDriver working properly to do much of the work I need to do on our site, except when new windows are launched. Seems there are a lot of options to handle this within Java, but I can't seem to make the leap to C# in how to collect the Window Handles and then switch to the newly spawned one. Part of the problem I am facing is that since it takes a few seconds for the new window to populate I am trying a wait to be able to capture the new window. When I originally did this I tried using

wait.Until(WebDriver => driver.Title.Equals("_blank"));
driver.SwitchTo().Window("_blank") 

to make sure the Window is spawned before trying to switch to it.

Then reading more on this I found I had to capture the windowHandles before making the switch, and since I still need to wait for the Window to spawn and populate I guess I need some kind of while to check for the new handle and switch to it, and at that point I am not sure what to do.

I'm using selenium2's .Net WebDriver, with SpecFlow so all the tests are in C# and I feel more comfortable in doing C# than Java, though I have not done much advanced work with C#. If anyone has some tips to get me started on how to resolve this I'd appreciate it.

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

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

发布评论

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

评论(1

兔小萌 2024-12-15 16:38:57

WebDriver.Support.dll 中的 .NET 支持类有一个 PopupWindowFinder 类,旨在帮助解决这种情况。目前它被列为实验性的,但至少,您可以检查 该类 看看它是如何工作的。如果它不能完全满足您的需求,它可能会给您一些关于如何编写自己的代码的见解。

The .NET support classes in WebDriver.Support.dll have a PopupWindowFinder class designed to help exactly this scenario. It's listed as experimental at the moment, but at the very least, you could examine the code of that class to see how it works. It might give you some insights on how to code your own, if it doesn't exactly meet your needs.

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