【C#】[网络]WebBrowser网页操作之提取获取元素和标签(完整篇)
在此基础上加强
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;//
namespace WebBrowser网页操作
{
public class Element
{
//根据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.getElementByIdx(id);
return e;
}
//根据Index获取元素
public HtmlElement GetElement_Index(WebBrowser wb,int index)
{
HtmlElement e = wb.Document.All[index];
return e;
}
// 据Type获取元 ,在没有NAME和ID的情况下使用public HtmlElement GetElement_Type(WebBrowser wb,string type){HtmlElement e=null;HtmlElementCollection elements = wb.Document.getElementsByTagName_r("input");foreach (HtmlElement element in elements){if (element.GetAttribute("type") == type){e = element;}}return e;}
// 据Type获取元 ,在没有NAME和ID的情况下使用,并指定是同类type的第 个,GetElement_Type()升级版public HtmlElement GetElement_Type_No(WebBrowser wb,string type,int i ){
int j=1;HtmlElement e=null;HtmlElementCollection elements = wb.Document.getElementsByTagName_r("input");foreach (HtmlElement element in elements){if (element.GetAttribute("type") == type){if(j==i)
{
e = element;
}
j++;}}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);
}
}
}
这是调用这个类的窗体代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WebBrowser网页操作
{
public partial class Form1 : Form
{
Element el = new Element();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate(Application.StartupPath + @"\Test.html");
}
private void button1_Click(object sender, EventArgs e)
{
el.Write_value(el.GetElement_Name(webBrowser1,"username"),"isaced");
}
private void button2_Click(object sender, EventArgs e)
{
el.Write_value(el.GetElement_Id(webBrowser1, "password"), "123456");
}
private void button3_Click(object sender, EventArgs e)
{
el.Btn_click(el.GetElement_Id(webBrowser1,"button"),"click");//方法用的按钮click
}
private void button4_Click(object sender, EventArgs e)
{
el.Btn_click(el.GetElement_Form(webBrowser1, "form1"), "submit");//先获取表单,再调用表单的submit方法
}
}
}
【C#】[网络]WebBrowser网页操作之提取获取元素和标签(完整篇)相关推荐
- html网页logo属性link,html元素link标签rel=icon添加网站favicon.ico图标
html元素中的link标签除了可以引用插入CSS样式表文件之外,还可以通过link标签设置网站的favicon.icon图标. 什么是favicon.icon图标? 网站favicon.ico图标, ...
- js 获取元素的几种方法
获取 元素/节点/标签 的几点方法 getElementById getElementsByTagName getElementsByName getByClass √ / ...
- Java使用Jsoup爬虫获取网站内容(三)获取元素内容属性的方法
介绍 我们可以通过Jsoup来获取一个Document对象,然后通过对Document对象进行解析来获取元素对象,最后通过元素对象的一些方法来获取元素的属性名称,属性值以及文本内容. 代码 附上获取方 ...
- webBrowser中操作网页元素全攻略
原文地址:https://www.cnblogs.com/qqflying/archive/2012/07/27/2611563.html 1.获取非input控件的值: webBrowser1.Do ...
- C# -爬虫之WebBrowser跨域跨iframe获取网页源码
前言:这里关键写用WebBrowser跨域跨iframe获取网页源码的部分,本意是要爬取全职高手的有声小说,这类网站特殊,网页上广告大堆,爬起来真麻烦,比如我爬取的网站的mp3源文件下载还需要秘钥的, ...
- 一个服务器 定时从各个系统取数据,网页定时从服务器获取数据库
网页定时从服务器获取数据库 内容精选 换一换 华为云提供两种连接方式通过SQL Server客户端连接实例:非SSL连接和SSL连接.其中,SSL连接实现了数据加密功能,具有更高的安全性.安装SQL ...
- Python实现自动化网页操作
编程语言:python 集成开发环境(IDE):Visual Studio Code 配置方法参照Visual Studio Code配置Python编程环境 目录 1 准备 1.1 安装seleni ...
- 网络Ghost克隆操作全程实例图解
网络Ghost克隆操作全程实例图解 QUOTE: 网络ghost在大批量装系统时效率非常高,二三百台微机,不出意外情况的话,半天到一天的时间就可以全部做完.通常用于学校.网吧.企业的计算机网络中客户机 ...
- GoAhead_网页操作
文章目录 1. GoAhead定时刷新获取后台数据 2. GoAhead 从网页提交数据给服务器 3. GoAhead Select 元素操作 4. GoAhead 提交input+select的值 ...
最新文章
- c语言exit在哪个头文件_C语言函数执行成功时,返回1和返回0,究竟哪个好?
- MarkDown写blog(测试)
- Linux for sougou ping yin (http://pinyin.sogou.com/linux/help.php)
- 【机器视觉】 dev_error_var算子
- 魅族8.0系统手机最完美激活xposed框架的步骤
- python学习-异常(异常类型,异常处理、自定义异常)
- 前端学习(1874)vue之电商管理系统电商系统之处理项目中eslint语法报错
- oracle数据库实践,RubyonRails连接Oracle数据库实践
- php注册树模式,PHP设计模式之注册树模式
- 《Linux4.0设备驱动开发详解》笔记--第五章:Linux文件系统与设备文件
- vim编辑器的快捷键使用
- python lncrna_分析指令备份.sh
- pytorch---tensor.view()
- Java基础零碎知识点总结(持续补充)
- Maxwell 是什么?
- git Pull Request 是什么意思?
- getch方法_linux 下getch()函数实现
- ESP8266从点灯到遥控小车(三)——组装小车,0代码开发APP控制小车
- 【MySql】windows下重置数据库密码
- ASP.net创建Controller视图时报错(Exception has been thrown by the target of an Invocation.)