本站消息

  出租广告位,需要合作请联系站长

  今日名言-想象你自己对困难作出的反应,不是逃避或绕开它们,而是面对它们,同它们打交道,以一种进取的和明智的方式同它们奋斗 。——马克斯威尔·马尔兹

  今日名言-用谅解、宽恕的目光和心理看人、待人。人就会觉得葱笼的世界里,春意盎然,到处充满温暖。——蔡文甫


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

C# webbrowser 获取网页元素 示例代码

发布于2021-07-24 21:41     阅读(1343)     评论(0)     点赞(15)     收藏(0)


在应用webbrowser对网页进行自动化操作时,不能有效地获取网页元素,往往是后续编程的拦路虎,原因是webbrowser提供的GetElementById()、GetElementFromPoint()、GetElementsByTagName()共3个方法,如果不仔细的研究,使用起来还真的是有些障碍。为了解决这些问题,我只要硬着头皮,费劲心思地研究了一番。至于说有什么成果没有,那就只有本文的读者自己体会了。下面用图片给大家展示一下,同时,把代码亮出来分享,以对大家有些小小的帮助。

网页元素要想看清楚些,用到的工具就是《开发人员工具》,看到了吧,浏览器都是自带的。

点击工具左上方的鼠标状按钮可以精确定位网页元素。

  程序运行效果

  1. 加载网页

 2、用ID查找搜索框,搜索框为有ID的input标签,显示对话框内容为该标签的类型

 聚焦到搜索框并填写“传奇霸主”字符串

 用标签内文本(innertext)查找标签,对话框框显示标签类型为超链接a。

 

 随后自动点击“全部游戏”超链接

  1. 查找iframe窗口中的元素方法。遗憾的是这里并没有写成单独的方法,而是直接放在了响应按钮的事件方法中。这主要是因为这里基本上是手工完成,如果要想遍历层次结构的iframe大概需要写一个递归的方法,可惜咱么太业余,完不成这个艰巨的任务。

首先是获得iframe窗口的句柄,然后再通过name属性查找输入框,最后写入内容

4、这个方法首先是做了一个确定元素在web中相对坐标的方法,然后根据显示内容,手工填写元素相对坐标在要编译的程序变量里。然后通过密码框在iframe中的相对位置获得了句柄,并写入密码。

 

下面是程序的源代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace wg5
  10. {
  11. public partial class Form1 : Form
  12. {
  13. public Form1()
  14. {
  15. InitializeComponent();
  16. }
  17. //用id查找元素
  18. private HtmlElement get_elem_by_id(WebBrowser w, string id) {
  19. return w.Document.GetElementById(id);
  20. }
  21. //用name属性查找元素,这个没有用到,因为是在iframe中查找name属性,所以手工做了
  22. private HtmlElement get_elem_by_name(WebBrowser w, string name) {
  23. return w.Document.All[name];
  24. }
  25. //用元素标签中的内容查找元素
  26. private HtmlElement get_elem_by_innertext(WebBrowser w, string innertext) {
  27. HtmlElement e = null;
  28. StringBuilder s = new StringBuilder();
  29. foreach (HtmlElement t in w.Document.All) {
  30. s.Clear();
  31. s.Append(t.InnerText);
  32. if (s.ToString().Equals(innertext)) {
  33. e = t;
  34. break;
  35. }
  36. }
  37. return e;
  38. }
  39. //查找元素的方法还有用type属性查找,用元素集合(bytagname)查找等,以上都是参考网上大咖,但觉得效果一般,因此没有列出
  40. private void button1_Click(object sender, EventArgs e)
  41. {
  42. //加载url,网页显示在web容器中
  43. webBrowser1.Navigate(textBox1.Text);
  44. }
  45. private void timer1_Tick(object sender, EventArgs e)
  46. {
  47. //获取鼠标在web容器的中的相对坐标
  48. // 第一个为鼠标相对屏幕的坐标,第二个为本程序窗口的坐标(窗口移动不影响计算)
  49. //第三个常量为预估的窗口边框的横纵向长度,第四个是web相对于窗口内边的距离(窗口设计时,已经设置为0)
  50. int x = Form1.MousePosition.X - this.Location.X - 8 - webBrowser1.Location.X;
  51. int y = Form1.MousePosition.Y - this.Location.Y - 30 - webBrowser1.Location.Y;
  52. label3.Text = x.ToString();
  53. label5.Text = y.ToString();
  54. }
  55. private void button2_Click(object sender, EventArgs e)
  56. {
  57. HtmlElement he = get_elem_by_id(webBrowser1, "search-input");
  58. if (he != null)
  59. {
  60. MessageBox.Show(he.TagName);
  61. he.Focus();
  62. he.SetAttribute("value", "传奇霸主");
  63. }
  64. else {
  65. MessageBox.Show("没有找到元素句柄!");
  66. }
  67. }
  68. private void button3_Click(object sender, EventArgs e)
  69. {
  70. HtmlElement he = get_elem_by_innertext(webBrowser1, "全部游戏");
  71. if (he != null)
  72. {
  73. MessageBox.Show(he.TagName);
  74. he.InvokeMember("click");
  75. }
  76. else
  77. {
  78. MessageBox.Show("没有找到元素句柄!");
  79. }
  80. }
  81. private void button4_Click(object sender, EventArgs e)
  82. {
  83. HtmlElement he = null;
  84. label6.Text = webBrowser1.Document.Window.Frames.Count.ToString();
  85. HtmlWindow hw = webBrowser1.Document.Window.Frames[1];
  86. he = hw.Document.All["userName"];
  87. if (he != null)
  88. {
  89. MessageBox.Show(he.TagName);
  90. he.SetAttribute("value", "ideal");
  91. }
  92. else
  93. {
  94. MessageBox.Show("没有找到元素句柄!");
  95. }
  96. }
  97. private void button5_Click(object sender, EventArgs e)
  98. {
  99. Point p = new Point();
  100. //这个数据是用鼠标手工测算的输入框(密码)相对于iframe窗口的距离,不用精确,鼠标落在元素上预估即可
  101. p.X = 77 - 18;
  102. p.Y = 287 - 172;
  103. HtmlWindow hw = webBrowser1.Document.Window.Frames[1];
  104. HtmlElement he = hw.Document.GetElementFromPoint(p);
  105. if (he != null)
  106. {
  107. MessageBox.Show(he.TagName);
  108. he.SetAttribute("value", "123456");
  109. }
  110. else
  111. {
  112. MessageBox.Show("没有找到元素句柄!");
  113. }
  114. }
  115. }
  116. }

原文链接:https://blog.csdn.net/weixin_39410618/article/details/118615563



所属网站分类: 程序员的那点事

作者:小飞刀你有点飘

链接:http://www.pythonpdf.com/blog/article/328/8cd5f1dbf610aaf30c54/

来源:编程知识网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

15 0
收藏该文
已收藏

评论内容:(最多支持255个字符)