【问题】 在C#模拟登陆12306网站的时候,遇到调用webbrowser打开地址: https://dynamic.12306.cn/otsweb/order/querySingleAction.do?method=init 会出现安全警告的弹出窗口: 窗口1:
窗口2: 上述两次弹出窗口,都点击确定后,结果还会弹出另外一个窗口: 窗口3: 然后,才能正常显示界面:
希望可以通过设置,不让这个security alert的窗口弹出,或通过程序代码模拟点击yes,以便不再麻烦用户点击。 【解决过程】 1.参考:Prevent Security alert in WebBrowser control,去设置了: wbsBuyTicket.ScriptErrorsSuppressed = true; 结果是窗口1和2,都还是继续会弹出,只能避免掉窗口3的弹出。 所以只是解决了部分的问题。 2.参考:How to disable “Security Alert” window in Webbrowser control,添加了:
但是还是没用,上述窗口1和2,仍会弹出。 3.网上找了一堆,还是没人给出有效解决办法。不过也是有人想到了,通过写程序,找到弹出的窗口,然后用程序去模拟点击yes。此方法,貌似很复杂,还是能不用则不用吧。 4.参考了:Easy way to prevent WebBrowser control from using window.alert,去添加对应代码: private void wbsBuyTicket_Navigated(object sender, WebBrowserNavigatedEventArgs e) { if (wbsBuyTicket.Document != null) { // kills messagebox functionality by disabling the "window.alert()" function object window = wbsBuyTicket.Document.Window.DomWindow; if (window != null) { Type windowType = window.GetType(); BindingFlags flags = BindingFlags.InvokeMethod | BindingFlags.Instance; string[] args = { "window.alert=function() {};", "JScript" }; windowType.InvokeMember("[DispID=1165]", flags, null, window, args); } // if } // if } 但是还是没用,因为在navigate之前,就弹出了窗口,所以此函数是在Navigated事件中处理的,所以没用。 也懒得再去试试其他事件,比如Navigating等,因为好像也是要根据程序代码去处理对应的特殊弹出窗口的代码的,此处没功夫去分析其中的代码。所以放弃此法。 |
|
来自: quasiceo > 《webbrowser安全警告》