加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 打开webbrowser,自动填写表单组件并提交

发布时间:2020-12-15 18:25:38 所属栏目:百科 来源:网络整理
导读:我们目前正在研究一种创建 WPF / winforms应用程序的方法,我们可以在内部设置它: 自动将Web浏览器的新实例打开到预定义的URL 使用预定义数据自动填写必填字段 自动提交表单并等待下一页加载 使用预定义数据自动填写必填字段(第2页) 自动提交表单并等待下一
我们目前正在研究一种创建 WPF / winforms应用程序的方法,我们可以在内部设置它:

>自动将Web浏览器的新实例打开到预定义的URL
>使用预定义数据自动填写必填字段
>自动提交表单并等待下一页加载
>使用预定义数据自动填写必填字段(第2页)
>自动提交表单并等待下一页加载(等)

经过多次调查,我们唯一能找到的就是通过以下方式打开网络浏览器: –

object o = null;

SHDocVw.InternetExplorer ie = new SHDocVw.InternetExplorer();
IWebBrowserApp wb = (IWebBrowserApp)ie;
wb.Visible = true;
wb.Navigate(url,ref o,ref o);

任何建议/阅读建议将不胜感激如何完成该过程.

解决方法

我写了一个例子来填充html页面中的元素.你必须做这样的事情:

Winform的

public Form1()
        {
            InitializeComponent();
            //navigate to you destination 
            webBrowser1.Navigate("https://www.certiport.com/portal/SSL/Login.aspx");
        }
        bool is_sec_page = false;
        private void webBrowser1_DocumentCompleted(object sender,WebBrowserDocumentCompletedEventArgs e)
        {
            if (!is_sec_page)
            {
                //get page element with id
                webBrowser1.Document.GetElementById("c_Username").InnerText = "username";
                webBrowser1.Document.GetElementById("c_Password").InnerText = "pass";
                //login in to account(fire a login button promagatelly)
                webBrowser1.Document.GetElementById("c_LoginBtn_c_CommandBtn").InvokeMember("click");
                is_sec_page = true;
            }
            //secound page(if correctly aotanticate
            else
            {
                //intract with sec page elements with theire ids and so on
            }

        }

WPF

public MainWindow()
        {
            InitializeComponent();
     webBrowser1.Navigate(new Uri("https://www.certiport.com/portal/SSL/Login.aspx"));
            }
            bool is_sec_page = false;
            mshtml.HTMLDocument htmldoc;
            private void webBrowser1_LoadCompleted(object sender,NavigationEventArgs e)
            {
                htmldoc = webBrowser1.Document as mshtml.HTMLDocument;
                if (!is_sec_page)
                {
                    //get page element with id
                    htmldoc.getElementById("c_Username").innerText = "username";
                    //or
                    //htmldoc.getElementById("c_Username")..SetAttribute("value","username");
                    htmldoc.getElementById("c_Password").innerText = "pass";
                    //login in to account(fire a login button promagatelly)
                    htmldoc.getElementById("c_LoginBtn_c_CommandBtn").InvokeMember("click");
                    is_sec_page = true;
                }
                //secound page(if correctly aotanticate
                else
                {
                    //intract with sec page elements with theire ids and so on
                }
            }

只需导航到特定的URL并填充页面元素.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读