问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/54652763/selenium-c-sharp-how-to-handle-alert-open-pick-an-app