C# webBrowser

webBrowser功能:

 

10、屏蔽脚本错误:

将WebBrowser控件ScriptErrorsSuppressed设置为True即可

webBrowser1.ScriptErrorsSuppressed = true;

 

                HtmlElementCollection input = this.webBrowser1.Document.GetElementsByTagName("input");//赋值
                for (int j = 0; j < input.Count; j++)
                {
                    if (input[j].GetAttribute("name").Equals("passWord", StringComparison.OrdinalIgnoreCase))
                    {
                        input[j].Document.GetElementById("passWord").SetAttribute("value", "admin");
                        break;
                    }
                }
                my.Pause(0.1f);
                HtmlElementCollection button = this.webBrowser1.Document.GetElementsByTagName("button");  //點擊button
                for (int j = 0; j < button.Count; j++)
                {
                    if (button[j].GetAttribute("type").Equals("submit", StringComparison.OrdinalIgnoreCase))
                    {
                        button[j].InvokeMember("click");
                        break;
                    }
                }

string webtext = webBrowser1.Document.All[0].InnerText;//取得网页里所有字符串信息

<a class="button" href="javascript:void(0);" οnclick="return modifyCommon(); ">Apply</a>

button[j].InnerText=="Apply";

InnerText就是找的文本的内容。

4、下拉、复选、多选:

复制代码
//下拉框:
secret.SetAttribute("value", "question1");  
//复选框
rememberme.SetAttribute("Checked", "True");
//多选框
cookietime.SetAttribute("checked", "checked");

 

 

 

16、获取元素、表单

复制代码
//根据Name获取元素
public HtmlElement GetElement_Name(WebBrowser wb,string Name)
{
    HtmlElement e = wb.Document.All[Name];
    return e;
}

//根据Id获取元素
public HtmlElement GetElement_Id(WebBrowser wb, string id)
{
    HtmlElement e = wb.Document.GetElementById(id);
    return e;
}

//根据Index获取元素
public HtmlElement GetElement_Index(WebBrowser wb,int index)
{
    HtmlElement e = wb.Document.All[index];
    return e;
}

//获取form表单名name,返回表单
public HtmlElement GetElement_Form(WebBrowser wb,string form_name)
{
    HtmlElement e = wb.Document.Forms[form_name];
    return e;
}


//设置元素value属性的值
public void Write_value(HtmlElement e,string value)
{
    e.SetAttribute("value", value);
}

//执行元素的方法,如:click,submit(需Form表单名)等
public void Btn_click(HtmlElement e,string s)
{

    e.InvokeMember(s);
}

14、让控件聚焦

this.webBrowser1.Select();
this.webBrowser1.Focus();
doc.All["TPL_password_1"].Focus();

 

http://www.cnblogs.com/txw1958/archive/2012/09/24/CSharp-WebBrowser.html

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值