zoukankan      html  css  js  c++  java
  • WebBrowser控件使用笔记

    1.webBrowser1_DocumentCompleted

            注意在网页加载完毕后操作,不可能找不到相关的元素

    2.取得并解析页面的所有链接,遍历找到目标链接,然后点击

                HtmlElementCollection links = this.webBrowser1.Document.Links;
                foreach (HtmlElement link in links)
                {
                    if (link.GetAttribute("href").Contains("abc.com"))
                    {
                       link.InvokeMember("click"); //激发链接的点击事件
                    }
                }
    

      

    3.用程序实现登陆

            ①Document.GetElementById()

            ②Document.All[ ]

            ③textboxUserId.SetAttribute("value";, userId)

            ④buttonSubmit.InvokeMember("click"),点击提交按钮来提交表单
            ⑤ this.webBrowser1.Document.Forms[0].InvokeMember("submit"),如果用这个方法可以跳过浏览器的客户端验证

               HtmlElement textboxUserId = this.webBrowser1.Document.GetElementById("登录用户名文本框的ID");
                //如果没有ID,用 Name 获取
                //HtmlElement textboxUserId = this.webBrowser1.Document.All["登录用户名文本框的Name"];
                HtmlElement textboxPassword = this.webBrowser1.Document.GetElementById("登录密码框的ID");
                //如果没有ID, 用Name 获取
                //HtmlElement textboxPassword = this.webBrowser1.Document.All["登录密码框的Name"];
                HtmlElement buttonSubmit = this.webBrowser1.Document.GetElementById("登录按钮的ID");
                //如果没有ID, 用Name获取
                //HtmlElement buttonSubmit = this.webBrowser1.Document.All["登录按钮的Name"];
                textboxUserId.SetAttribute("value", userId);      //填写帐号
                textboxPassword.SetAttribute("value", password);    //填写密码
                buttonSubmit.InvokeMember("click");               //触发提交按钮的点击事件
                //当然,登录,也可以用
                //this.webBrowser1.Document.Forms[0].InvokeMember("submit");
                //来实现,但是,上面的语句,会跳过浏览器客户端验证函数(如果有的话)
    
                //登录后,需判断登录是否成功, 可以根据登录后的 URL 
                //或者 this.webBrowser1.Document.Body.InnerHtml的内容来判断
    本博客(liqipeng)除非已明确说明转载,否则皆为liqipeng原创或者整理,转载请保留此链接:https://www.cnblogs.com/liqipeng/archive/2012/07/02/4576217.html

    本博客(liqipeng)除非已明确说明转载,否则皆为liqipeng原创或者整理,转载请保留此链接:https://www.cnblogs.com/liqipeng/archive/2012/07/02/4576217.html
    如果你觉得这篇文章对你有帮助或者使你有所启发,请点击右下角的推荐按钮,谢谢,:)
  • 相关阅读:
    vue CDN seting
    AutoMapper
    c# list<class> to datatable
    vue watch
    net core 3.1 open file
    net core 3.1 area Global
    net core 3.1 area
    ef core log
    3.1 ef core
    3.1 daper
  • 原文地址:https://www.cnblogs.com/liqipeng/p/4576217.html
Copyright © 2011-2022 走看看