1、获取非input控件的值:
webBrowser1.Document.All["控件ID"].InnerText;
或webBrowser1.Document.GetElementById("控件ID").InnerText;
或webBrowser1.Document.GetElementById("控件ID").GetAttribute("value");
2、获取input控件的值:
webBrowser1.Document.All["控件ID"].GetAttribute("value");;
或webBrowser1.Document.GetElementById("控件ID").GetAttribute("value");
3、给输入框赋值:
webBrowser1.Document.GetElementById("控件ID").SetAttribute("value", "控件值");
4、CheckBox选中:
webBrowser1.Document.GetElementById("控件ID").SetAttribute("Checked", "true");
5、触发select事件
select赋值:
webBrowser1.Document.GetElementById("控件ID").SetAttribute("selected", "selected");
webBrowser1.Document.GetElementById("控件ID").SetAttribute("value", "控件值");
触发事件:
1)webBrowser1.Document.GetElementById("控件ID").SetAttribute("selectedIndex", “选项序号”);
2)HtmlElement selScript = doc.CreateElement("script");
selScript.SetAttribute("type", "text/javascript");
/注释/// ajax//
///selScript.SetAttribute("text", "function _seloption(){$.ajax({url:'http://ncp.nifdc.org.cn/AjaxHandler/GetFoodTypeThird.ashx',type:'get',data:{parent_id:'948'},success:function(data){if(data!=null){$('#slt_food_type_third').empty();$('#slt_food_type_third').append(data);},complete:function(){}});}");
/注释 ajax/
selScript.SetAttribute("text", "function _seloption(){$('#" + idname + "').trigger('change')}");
HtmlElement head = doc.Body.AppendChild(selScript);
doc.InvokeScript("_seloption");
6、根据已知有ID的元素操作没有ID的元素:
HtmlElement btnDelete = webBrowser1.Document.GetElementById(passengerId).Parent.Parent.Parent.Parent.FirstChild.FirstChild.Children[1].FirstChild.FirstChild;
根据Parent,FirstChild,Children[1]数组,多少层级的元素都能找到。
7、获取Div或其他元素的样式:
webBrowser1.Document.GetElementById("addDiv").Style;
8、直接执行页面中的脚本函数,带动态参数或不带参数都行:
Object[] objArray = new Object[1];
objArray[0] = (Object)this.labFlightNumber.Text;
webBrowser1.Document.InvokeScript("ticketbook", objArray);
webBrowser1.Document.InvokeScript("return false");
9、自动点击、自动提交:
HtmlElement btnAdd = doc.GetElementById("addDiv").FirstChild;
btnAdd.InvokeMember("Click");
10、自动赋值,然后点击提交按钮的时候如果出现脚本错误或一直加载的问题,一般都是点击事件执行过快,这时需要借助Timer控件延迟执行提交按钮事件:
this.timer1.Enabled = true;
this.timer1.Interval = 1000 * 2;
private void timer1_Tick(object sender, EventArgs e)
{
this.timer1.Enabled = false;
ClickBtn.InvokeMember("Click");//执行按扭操作
}
11、屏蔽脚本错误:
将WebBrowser控件ScriptErrorsSuppressed设置为True即可
12、自动点击弹出提示框:
参考:http://www.cnblogs.com/qqflying/archive/2012/07/25/2608038.html
13、获取网页中的Iframe,并设置Iframe的src
HtmlDocument docFrame = webBrowser1.Document.Window.Frames["mainFrame"].Document;
或
HtmlDocument docFrame = webBrowser1.Document.All.Frames["mainFrame"].Document;
docFrame.All["mainFrame"].SetAttribute("src", "http://www.baidu.com/");
14、网页中存在Iframe的时候webBrowser1.Url和webBrowser1_DocumentCompleted中的e.Url不一样,前者是主框架的Url,后者是当前活动框口的Url。
15、让控件聚焦
this.webBrowser1.Select();
this.webBrowser1.Focus();
doc.All["TPL_password_1"].Focus();
16、获取class
// he 是HtmlElement对象
// GetAttribute("class") 一直取空值
he.GetAttribute("className")
转载自:https://blog.csdn.net/bluecard2008/article/details/84312612?utm_medium=distribute.pc_relevant_bbs_down.none-task-blog-baidujs-1.nonecase&depth_1-utm_source=distribute.pc_relevant_bbs_down.none-task-blog-baidujs-1.nonecase
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现