微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我们目前正在研究一种创建 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并填充页面元素.

原文地址:https://www.jb51.cc/csharp/98497.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐