http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 5316|回复: 5

[求助] WebBrowser操作页面时最后出现一个提示框如何自动点击确认

[复制链接]
发表于 2014-1-7 17:53:05 | 显示全部楼层 |阅读模式
WebBrowser操作页面时最后出现一个提示框,是否确认。
有没有办法自动点击这个提示框的“是”。
因为这个提示框一出现后面的代码就不走了,而且也无法获得这个提示框的元素。
站长大人,有没有什么办法。自动确认


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2014-2-24 21:10:55 | 显示全部楼层
这个也是我碰到的问题,我是通过多线程解决的,判断新窗口出现 获得句柄再去关闭,暂时只能这样,我找了很多没有什么办法 ,我尝试只有多线程能解决,还有就是现在我觉得网页操作不方便 ,找dom看脚本麻烦,还不如用 httphelper去模拟网页post,不过有时候post我怕非法数据会不会对网站有啥影响。
发表于 2014-2-24 21:15:46 | 显示全部楼层
通过多线程来解决,我之前也碰到这样的问题,好像没有什么完美的解决办法,我觉得找dom和看脚本不如用站长的httphelper来模拟post比较方便,就是有时候怕提交非法数据把网站搞死了,不知道会不会!
发表于 2014-8-22 12:59:58 | 显示全部楼层
用这个就可以了,这个我稍微改了下回传提示消息的
[C#] 纯文本查看 复制代码
/****************************************************************************

Tilde

Copyright (c) 2008 Tantalus Media Pty

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

****************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XXX
{
    public class MyWebBrowser : System.Windows.Forms.WebBrowser
    {

        string lpstrText = string.Empty;
        /// <summary>
        /// JS弹出提示的内容
        /// </summary>
        public string LpstrText
        {
            get
            {
                string tmpStr = lpstrText;
                lpstrText = string.Empty;
                return tmpStr;
            }
            set { lpstrText = value; }
        }

        #region ExtendedWebBrowserSite
        class ExtendedWebBrowserSite : WebBrowser.WebBrowserSite, UnsafeNativeMethods.IDocHostShowUI
        {
            int webIndex;
            public ExtendedWebBrowserSite(WebBrowser host, int webIndex)
                : base(host)
            {
                this.webIndex = webIndex;
            }
            void UnsafeNativeMethods.IDocHostShowUI.ShowMessage(ref UnsafeNativeMethods._RemotableHandle hwnd, string lpstrText, string lpstrCaption, uint dwType, string lpstrHelpFile, uint dwHelpContext, out int plResult)
            {
                plResult = 0;
                //TODO:自定义 
                GlobalVariable.LpstrText[webIndex] = lpstrText;
            }
            void UnsafeNativeMethods.IDocHostShowUI.ShowHelp(ref UnsafeNativeMethods._RemotableHandle hwnd, string pszHelpFile, uint uCommand, uint dwData, UnsafeNativeMethods.tagPOINT ptMouse, object pDispatchObjectHit)
            {
                //TODO:自定义 
            }
        }

        /// <summary>
        /// 当前控件的线程序号
        /// </summary>
        public int WebIndex
        {
            get;
            set;
        }
        protected override WebBrowserSiteBase CreateWebBrowserSiteBase()
        {
            return new ExtendedWebBrowserSite(this, WebIndex);
        }
        #endregion
    }

    public class UnsafeNativeMethods
    {
        #region IDocHostShowUI
        [StructLayout(LayoutKind.Explicit, Pack = 4)]
        public struct __MIDL_IWinTypes_0009
        {
            // Fields 
            [FieldOffset(0)]
            public int hInproc;
            [FieldOffset(0)]
            public int hRemote;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 4)]
        public struct _RemotableHandle
        {
            public int fContext;
            public __MIDL_IWinTypes_0009 u;
        }

        [StructLayout(LayoutKind.Sequential, Pack = 4)]
        public struct tagPOINT
        {
            public int x;
            public int y;
        }

        [ComImport, Guid("C4D244B0-D43E-11CF-893B-00AA00BDCE1A"), InterfaceType((short)1)]
        public interface IDocHostShowUI
        {
            [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
            void ShowMessage([In, ComAliasName("ExtendedWebBrowser2.UnsafeNativeMethods.wireHWND")] ref _RemotableHandle hwnd, [In, MarshalAs(UnmanagedType.LPWStr)] string lpstrText, [In, MarshalAs(UnmanagedType.LPWStr)] string lpstrCaption, [In] uint dwType, [In, MarshalAs(UnmanagedType.LPWStr)] string lpstrHelpFile, [In] uint dwHelpContext, [ComAliasName("ExtendedWebBrowser2.UnsafeNativeMethods.LONG_PTR")] out int plResult);
            [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
            void ShowHelp([In, ComAliasName("ExtendedWebBrowser2.UnsafeNativeMethods.wireHWND")] ref _RemotableHandle hwnd, [In, MarshalAs(UnmanagedType.LPWStr)] string pszHelpFile, [In] uint uCommand, [In] uint dwData, [In] tagPOINT ptMouse, [Out, MarshalAs(UnmanagedType.IDispatch)] object pDispatchObjectHit);
        }
        #endregion

    } 
}

发表于 2014-8-22 15:40:41 | 显示全部楼层
把这个添加到加载完成事件中就可以解决了,另外有多余的js函数 自行添加到StringBuilder中
[C#] 纯文本查看 复制代码
private void wb_moluren_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //*
            WebBrowser wbWebBrowser = (WebBrowser)sender;
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("function alert(){return;}");
            sb.AppendLine("function confirm(){return true;}");
            sb.AppendLine("function showModalDialog(){return;}");
            sb.AppendLine("function window.open(){return;}");
            sb.AppendLine("function prompt(){return;}");
            string strJS = sb.ToString();
            IHTMLWindow2 win = (IHTMLWindow2)wbWebBrowser.Document.Window.DomWindow;
            win.execScript(strJS, "Javascript");
            win = null;
        }

发表于 2014-8-26 10:44:22 | 显示全部楼层
真是难得给力的帖子啊。
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-29 21:30

© 2014-2021

快速回复 返回顶部 返回列表