【问题标题】:Find text in webbrowser control在浏览器控件中查找文本
【发布时间】:2017-07-30 06:35:01
【问题描述】:

我有一个网络浏览器控件,已经导航到一个页面。现在我想在页面中搜索特定的文本,然后滚动到第一个出现的地方。

能够滚动到其他事件是一种奖励。

【问题讨论】:

    标签: c# winforms webbrowser-control


    【解决方案1】:

    你可以试试这个代码:

    webBrowser1.Select(); SendKeys.Send("^f");

    【讨论】:

      【解决方案2】:

      我不知道它是否适用于WebBroswer。我们使用以下代码使浏览器(IE/FF/etc)窗口滚动到一些文本:

      //source code of http://www.sample.com/sample.html
      <html>
      ...
      <span name="aim">KeyWord</span>
      ...
      </html>
      

      如果我想让窗口滚动到“关键字”,只需访问http://www.sample.com/sample.html#aim

      使用string document = myWebBrowser.DocumentText获取页面的源代码,并在字符串中搜索文本,获取其节点名称,并使用#进行导航。

      【讨论】:

      • 其实问题不是滚动到特定节点;这已经完成了。找到与搜索查询匹配的文本选择是主要问题。
      • 这里必须添加一点细节...在文本中搜索会在字符串中生成索引。找到包含该字符串的节点并不是那么简单:匹配的文本可能是多个文本节点合并到一个节点中的结果。
      【解决方案3】:

      如果有帮助,请查看:

      string PageSource = null;
      PageSource = WebBrowser1.Document.Body.InnerHtml();
      if (Strings.InStr(PageSource, stringtoFind) > 0) {
        ...insert an Anchor tag here and then use 
         WebBrowser1.Navigate to go to the the new URL with the #Anchor tag
      } else {
      ...whatever else
      }
      

      【讨论】:

        【解决方案4】:

        一种方式...

        使用 Ctrl + F 键调用 WebBrowser 控件原生的 Find

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-05-11
          • 2012-04-10
          • 2011-09-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多