Selenium C# 如何处理警报“打开选择一个应用程序"? [英] Selenium C# How to handle Alert "Open Pick an app"?

查看:41
本文介绍了Selenium C# 如何处理警报“打开选择一个应用程序"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Selenium C# for Chrome 获取警报文本

但是这个警报似乎与普通的 JavaScript 警报不同,使用下面的代码返回一个错误异常:

string text = driver.SwitchTo().Alert().Text;

<块引用>

OpenQA.Selenium.NoAlertPresentException:没有这样的警报

这是否被视为警报?有办法处理吗?

解决方案

您看到的对话框显示在操作系统级别,因此您无法在 DOM 中看到它或使用 Selenium 与之交互.如果您真的需要处理它,我建议您研究一种操作系统级自动化工具,该工具可以发送按键或鼠标事件以您需要的方式处理对话框.

对于 Java,您可以使用 在 Java AWT 包中定义的 Robot 类,因为它专门用于处理与测试自动化相关的本机输入事件,并且在处理 Selenium 和对话框时经常被引用.但是,它在 .NET 世界中并不存在,因此您需要查看其他地方.一个快速的谷歌搜索给了我一些可能有帮助的东西:

机器人框架(使用 IronPython 支持 .NET)

另外,你可以用这样的方法更简单:

GlobalMouseKeyHook

总结一下.不,这不是您期望的正常警报,是的,您可以处理它,但需要在 Selenium 世界之外采取一些步骤.

I am trying to get the Alert's Text using Selenium C# for Chrome

But this alert seems different from a normal JavaScript Alert, using the code below returns an error exception:

string text = driver.SwitchTo().Alert().Text;

OpenQA.Selenium.NoAlertPresentException: no such alert

Is this considered as an alert? Is there a way to handle it?

解决方案

The dialog you're seeing is shown on the OS level so there's no way for you to see that in the DOM or use Selenium to interact with it. If you really need to deal with it, I'd suggest you look into an OS-level automation tool that could send key or mouse events to handle the dialog in a way you need to.

With Java, you would be fine using the Robot class defined in the Java AWT package, as it's meant exactly for dealing with native input events associated with test automation and is often referred to when dealing with Selenium and dialogs. However, it doesn't exist in the .NET world so you need to look elsewhere. A quick Google search gave me something that might help:

Robot Framework (Supports .NET by using IronPython)

Also, you could do it even simpler with something like this:

GlobalMouseKeyHook

So, to summarize. No, it's not a normal Alert as you expect it to be and yes, you can handle it but it requires some steps outside the Selenium world.

这篇关于Selenium C# 如何处理警报“打开选择一个应用程序"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆