如何使用wpf从web浏览器控件中的任何标记中获取值

本文关键字:任何标 获取 控件 wpf 何使用 web 浏览器 | 更新日期: 2023-09-27 18:22:04

我在一个字符串类型的变量中有一个完整的网页源代码。

这些代码如下所示:

<!DOCTYPE html>
<html lang="en" dir="ltr"
      class="">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;">
    <title>Twitter / Authorize an application</title>
    <link href="https://abs.twimg.com/a/1373417125/tfw/css/tfw.bundle.css" media="screen" rel="stylesheet" type="text/css" />
    <!--[if (IEMobile) & (lt IE 9)]>
    <link href="https://abs.twimg.com/a/1373417125/tfw/css/tfw_iemobile.bundle.css" media="screen" rel="stylesheet" type="text/css" />
    <![endif]-->
    
    <style type="text/css">
      html { display:none; }
    </style>
    <noscript>
      <style type="text/css">
        html { display: block; }
      </style>
    </noscript>
    <!--[if IEMobile & lt IE 9]>
    <style type="text/css">
      html { display: block; }
    </style>
    <![endif]-->
  </head>
  <body class="oauth
 write
 signed-in
 tfw
 en
 logged-in
 
 
 noloki
">
    <div id="header" role="banner">
      <div class="bar">
        <h1 class="logo"><a href="https://twitter.com/home" class="alternate-context">Twitter</a></h1>
        
        <div id="session" role="navigation">
          <h2 class="current-user"><a href="https://twitter.com/xyz" title="Ankita Kumar">
            <img src="https://twimg0-a.akamaihd.net/profile_images/37880007865454313/7c577aeb3085dasf355dgf76f043c1cd09_mini.jpeg" alt="" width="24" height="24">
            <span class="name">ankita_yahoo</span>
          </a></h2>
          <div class="user-menu">
            <h3 class="session">User Management</h3>
            <ul class="session">
              <li class="new"><a href="/intent/tweet">New Tweet</a></li>
              <li class="settings"><a href="/settings">Settings</a></li>
              <li class="help"><a href="/help">Help</a></li>
              
                <li class="signout"><form action="/intent/session" method="post"><div style="margin:0;padding:0"><input name="_method" type="hidden" value="delete" /><input name="authenticity_token" type="hidden" value="015cfb030df22330e517d50b0770c4dab7f3f89b" /></div>
                    <input id="referer" name="referer" type="hidden" value="/oauth/authorize" />
                    <input type="submit" class="textual link" value="Sign out">
                </form></li>
              
            </ul>
          </div>
        </div>
        
      </div>
    </div>
    <div id="bd" role="main">
      
<p class="action-information">You've granted access to ThisTweetControl!</p>
<div id="oauth_pin">
  <p>
    <span id="code-desc">Next, return this PIN to complete the authorization process:</span>
    <kbd aria-labelledby="code-desc"><code>6107346</code></kbd>
  </p>
</div>

    </div>
    
    
    <div class="footer" role="navigation"><div id="ft">
      
  <a href="https://twitter.com/home" class="return-link alternate-context">Go to Twitter</a>
  <a href="http://www.xyz.biz" class="supplemental-link alternate-context">Go to the ThisTweetControl homepage</a>
<p class="tip">You can revoke access to any application at any time from the <a href="https://twitter.com/settings/applications">Applications tab</a> of your Settings page.</p>
<p><small>By authorizing an application you continue to operate under <a href="https://twitter.com/tos">Twitter's Terms of Service</a>. In particular, some usage information will be shared back with Twitter. For more, see our <a href="https://twitter.com/privacy">Privacy Policy</a>.</small></p>
    </div></div>
    
    <script type="text/javascript" charset="utf-8">
        var twttr = twttr || {};
        twttr.form_authenticity_token = '015cfb03a75wdsfgdf6753dsfb0770c4dab7f3f89b';
        if (self == top) {
            document.documentElement.style.display = 'block';
        }
    </script>
    <script src="https://abs.twimg.com/a/14353417125/javascripts/loadrunner.js" data-main="tfw/intents/main" data-path="https://abs.twimg.com/a/1373417125/javascripts/modules" type="text/javascript"></script>
    
    
    

  </body>
</html>

在上面的代码行中,有一个带有某个值的代码标记<code>6107346</code>,我需要获得变量中的数值。

如何使用wpf从web浏览器控件中的任何标记中获取值

如果您使用System.Windows.Controls.WebBrowser控件,那么这应该可以工作,但是它是在假设您只有一个CODE标签的情况下工作的:

var myTagValue = (wbBrowser.Document as mshtml.HTMLDocument)
                   .getElementsByTagName("CODE")
                   .OfType<mshtml.IHTMLElement>()
                   .First()
                   .innerText

您需要参考Internet Explorer的COM库:Microsoft HTML Object LibrarygetElementsByTagName返回所有匹配标签的列表,无论它们在HTML 中的位置如何