WebBrowser ID访问 - 不在DocumentText中,而是在页面源中

时间:2016-03-08 20:52:44

标签: c# html dom com webbrowser-control

我正在使用WebBrowser对象,用于Windows窗体.Net Framework 4.5应用程序的Web自动化目的。在某个页面中,即使我从实际浏览器中打开查看页面来源,我也可以看到WebBrowser.DocumentText中的ID。

这是我一直用来获取页面内HtmlElement的代码。

var attack = wb.Document.GetElementById("target_attack");

总结如何检索不在DocumentText内但在页面源内的ID?

修改:这是页面来源数据中的样子。只需按控制+ f ,然后搜索名为troop_confirm_go的ID:

<table align="center" id="contentContainer" width="100%">
        <tr>
            <td>
                <table class="content-border" width="100%" cellspacing="0">
                    <tr>
                        <td id="inner-border">
                            <table class="main" align="left">
                                <tr>
                                    <td id="content_value">


                                        <form id="command-data-form" action="/game.php?village=39143&amp;screen=place&amp;action=command&amp;h=c86dc3bd" method="post" onsubmit="this.submit.disabled=true;">
<input type="hidden" name="attack" value="true" />

<h2>Barbar köyü hedefindeki saldırıyı onayla</h2>


<input type="hidden" name="ch" value="b5aa6baff4c83d16a08edb7709204aefd3e85650" />
<input type="hidden" name="x" value="489" />
<input type="hidden" name="y" value="690" />
<input type="hidden" name="source_village" value="39143">
<input type="hidden" name="action_id" value="1880595" />

<table class="vis" width="300">
    <tr>
        <th  colspan="2">
                            Komut
                        </th>
                </tr>
    <tr><td>Hedef:</td><td><span class="village_anchor" data-player="0" data-id="42165"><a href="/game.php?village=39143&amp;screen=info_village&amp;id=42165" >Barbar köyü (489|690) K64</a></span></td></tr>
            <tr><td>Süre:</td><td>0:40:15</td></tr>
    <tr>
        <td>Varış:</td>
        <td id="date_arrival">
            <span class="relative_time" data-duration="2415">
            bugün saat 23:46 
            </span>
                        </td>
    </tr>
    <tr><td>Moral:</td><td><b>100%</b></td></tr>                <tr><td colspan="2"><span class="icon header ressources" title="Taşıma kapasitesi"></span>250</td></tr>    </table><br />

<table class="vis" id="place_confirm_units">
    <tr>
        <th class="train-ui" style="display: none"></th>
        <th width="50"><a href="#" class="unit_link" data-unit="spear"><img src="https://dstr.innogamescdn.com/8.44.1/28599/graphic/unit/unit_spear.png" title="Mızrakçı" alt="" class="" /></a></th><th width="50"><a href="#" class="unit_link" data-unit="sword"><img src="https://dstr.innogamescdn.com/8.44.1/28599/graphic/unit/unit_sword.png" title="Kılıç Ustası" alt="" class="faded" /></a></th><th width="50"><a href="#" class="unit_link" data-unit="axe"><img src="https://dstr.innogamescdn.com/8.44.1/28599/graphic/unit/unit_axe.png" title="Baltacı" alt="" class="faded" /></a></th><th width="50"><a href="#" class="unit_link" data-unit="spy"><img src="https://dstr.innogamescdn.com/8.44.1/28599/graphic/unit/unit_spy.png" title="Casus" alt="" class="faded" /></a></th><th width="50"><a href="#" class="unit_link" data-unit="light"><img src="https://dstr.innogamescdn.com/8.44.1/28599/graphic/unit/unit_light.png" title="Hafif atlı" alt="" class="faded" /></a></th><th width="50"><a href="#" class="unit_link" data-unit="heavy"><img src="https://dstr.innogamescdn.com/8.44.1/28599/graphic/unit/unit_heavy.png" title="Ağır atlı" alt="" class="faded" /></a></th><th width="50"><a href="#" class="unit_link" data-unit="ram"><img src="https://dstr.innogamescdn.com/8.44.1/28599/graphic/unit/unit_ram.png" title="Şahmerdan" alt="" class="faded" /></a></th><th width="50"><a href="#" class="unit_link" data-unit="catapult"><img src="https://dstr.innogamescdn.com/8.44.1/28599/graphic/unit/unit_catapult.png" title="Mancınık" alt="" class="faded" /></a></th><th width="50"><a href="#" class="unit_link" data-unit="knight"><img src="https://dstr.innogamescdn.com/8.44.1/28599/graphic/unit/unit_knight.png" title="Şövalye" alt="" class="faded" /></a></th><th width="50"><a href="#" class="unit_link" data-unit="snob"><img src="https://dstr.innogamescdn.com/8.44.1/28599/graphic/unit/unit_snob.png" title="Misyoner" alt="" class="faded" /></a></th>
                </tr>
    <tr class="train-ui" style="display: none">
        <th>Köy birimleri</th>
        <td class='unit-item unit-item-spear '>10</td><td class='unit-item unit-item-sword '>10</td><td class='unit-item unit-item-axe hidden'>0</td><td class='unit-item unit-item-spy hidden'>0</td><td class='unit-item unit-item-light hidden'>0</td><td class='unit-item unit-item-heavy hidden'>0</td><td class='unit-item unit-item-ram hidden'>0</td><td class='unit-item unit-item-catapult hidden'>0</td><td class='unit-item unit-item-knight hidden'>0</td><td class='unit-item unit-item-snob hidden'>0</td>
    </tr>
    <tr class="units-row">
        <th class="train-ui train-name" style="display: none"><span>Saldırı #1</span></th>
        <td class='unit-item unit-item-spear '>10</td><td class='unit-item unit-item-sword hidden'>0</td><td class='unit-item unit-item-axe hidden'>0</td><td class='unit-item unit-item-spy hidden'>0</td><td class='unit-item unit-item-light hidden'>0</td><td class='unit-item unit-item-heavy hidden'>0</td><td class='unit-item unit-item-ram hidden'>0</td><td class='unit-item unit-item-catapult hidden'>0</td><td class='unit-item unit-item-knight hidden'>0</td><td class='unit-item unit-item-snob hidden'>0</td>
                </tr>
    <tr class="train-ui units-sum" style="display: none">
        <th>Toplam</th>
        <td class='unit-item unit-item-spear '>10</td><td class='unit-item unit-item-sword hidden'>0</td><td class='unit-item unit-item-axe hidden'>0</td><td class='unit-item unit-item-spy hidden'>0</td><td class='unit-item unit-item-light hidden'>0</td><td class='unit-item unit-item-heavy hidden'>0</td><td class='unit-item unit-item-ram hidden'>0</td><td class='unit-item unit-item-catapult hidden'>0</td><td class='unit-item unit-item-knight hidden'>0</td><td class='unit-item unit-item-snob hidden'>0</td>
    </tr>
</table>

<br />

    <input type="hidden" name="spear" value="10" />
    <input type="hidden" name="sword" value="0" />
    <input type="hidden" name="axe" value="0" />
    <input type="hidden" name="spy" value="0" />
    <input type="hidden" name="light" value="0" />
    <input type="hidden" name="heavy" value="0" />
    <input type="hidden" name="ram" value="0" />
    <input type="hidden" name="catapult" value="0" />
    <input type="hidden" name="knight" value="0" />
    <input type="hidden" name="snob" value="0" />

<h3 class="error" id="train_noble_warning" style="display: none">
    Misyonerler koruma olmadan gönderilmemeli!
</h3>

<input id="troop_confirm_go" style="margin-bottom: 5px" class="troop_confirm_go btn btn-attack" name="submit" type="submit" onload="this.disabled=false;" value="Saldırı gönder" />
<a href="#" id="troop_confirm_train" class="btn btn-img" style="display: none; line-height: 21px">
    <img src="https://dstr.innogamescdn.com/8.44.1/28599/graphic/unit/tiny/snob.png" title="" alt="" class="" /> Misyoner saldırısı ekle
</a>
</form>

 <script type="text/javascript">
 $(document).ready(function() {
    $('#troop_confirm_go').focus();
            Place.confirmScreen.init({"type":"attack","units":["spear","sword","axe","spy","light","heavy","ram","catapult","knight","snob"],"send_units":{"spear":10,"sword":0,"axe":0,"spy":0,"light":0,"heavy":0,"ram":0,"catapult":0,"knight":0,"snob":0},"available_units":{"spear":10,"sword":10,"axe":0,"spy":0,"light":0,"heavy":0,"ram":0,"catapult":0,"knight":0,"snob":0}});
        });
</script>
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>

    <p class="server_info">
        Sunucu saati: <span id="serverTime">23:05:56</span> <span id="serverDate">08/03/2016</span>
        <span id="dev_console" style="display: none"><b>|</b> <a href="?screen=74:72:69:62:61:6c:77:61:72:73">developer console</a></span>
                </p>

</td>
<td class="bg_right" id="SkyScraperAdCell">
    <div class="bg_right"> </div>
                <div id="SkyScraperAd">
            <script type='text/javascript'><!--//<![CDATA[
var m3_u = (location.protocol=='https:'?'https://openx.innogames.de/delivery/ajs.php':'http://openx.innogames.de/delivery/ajs.php');
var m3_r = Math.floor(Math.random()*99999999999);
if (!document.MAX_used) document.MAX_used = ',';
document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
document.write ("?zoneid=1620");
document.write ('&amp;cb=' + m3_r);
if (document.MAX_used != ',') document.write ("&amp;exclude=" + document.MAX_used);
document.write (document.charset ? '&amp;charset='+document.charset : (document.characterSet ? '&amp;charset='+document.characterSet : ''));
document.write ("&amp;loc=" + escape(window.location));
if (document.referrer) document.write ("&amp;referer=" + escape(document.referrer));
if (document.context) document.write ("&context=" + escape(document.context));
if (document.mmm_fo) document.write ("&amp;mmm_fo=1");
document.write ("'><\/scr"+"ipt>");
//]]>--></script><noscript><a href='http://openx.innogames.de/delivery/ck.php?n=a47300e4&amp;cb=INSERT_RANDOM_NUMBER_HERE' target='_blank'><img src='http://openx.innogames.de/delivery/avw.php?zoneid=1620&amp;cb=INSERT_RANDOM_NUMBER_HERE&amp;n=a47300e4' border='0' alt='' /></a></noscript>

        </div>
        </td>
</tr>

                    <tr>
            <td class="bg_leftborder"> </td>
            <td></td>
            <td class="bg_rightborder"> </td>
        </tr>
        <tr class="newStyleOnly">
            <td class="bg_bottomleft">&nbsp;</td>
            <td class="bg_bottomcenter">&nbsp;</td>
            <td class="bg_bottomright">&nbsp;</td>
        </tr>
                    </table><!-- .main_layout -->


<script type="text/javascript">

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-1897727-23']);
_gaq.push(['_trackPageview']);
_gaq.push(['_gat._anonymizeIp']);


(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
 (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(ga);
})();

</script>


            <div id="world_selection_clicktrap" class="evt-world-selection-toggle">
    </div>
    <div id="world_selection_popup">
        <div class="servers-list-top"></div>
        <div id="servers-list-block">

        </div>
        <div class="servers-list-bottom"></div>
    </div>

    <div class="chat-open-buffer"></div>

    <div id="bottom">
        <div id="chat-wrapper">
        </div>

        <div id="footer">
            <a href="#" id="unsupported-browser">
                <img src="https://dstr.innogamescdn.com/8.44.1/28599/graphic/error.png" title="" alt="" class="" /> Desteklenmeyen tarayıcı
            </a>
            <div id="linkContainer">
                                    <a href="#" class="world_button_active evt-world-selection-toggle">32. Dünya</a>
                                    <a href="http://forum.klanlar.org/index.php" class="footer-link" target="_blank">Forum</a>
                &nbsp;-&nbsp;
                <a href="http://help.klanlar.org/wiki/Ana_sayfa" class="footer-link" target="_blank">Yardım</a>
                                        &nbsp;-&nbsp;
                    <a href="http://blog.klanlar.org" class="footer-link" target="_blank">Blog</a>
                                                        &nbsp;-&nbsp;
                <a href="/game.php?village=39143&amp;screen=settings&amp;mode=ticket" class="footer-link" target="_blank">Destek</a>
                                                            &nbsp;-&nbsp;
                    <a href="/game.php?village=39143&amp;screen=settings&amp;mode=ref&amp;source=bottom_menu" class="footer-link">Oyuncu davet et</a>
                                                        &nbsp;-&nbsp;
                <a href="/game.php?village=39143&amp;screen=memo" class="footer-link">Not defteri</a>
                                                                            &nbsp;-&nbsp;
                <a href="/game.php?village=39143&amp;screen=&amp;action=logout&amp;h=c86dc3bd" target="_top" class="footer-link">Çıkış</a>
                                </div>
        </div>
    </div>




<script>
$(document).ready(function() {
    Timing.init(1457471156.4343);
    TribalWars.initTab('1458e0d83d');



        WorldSwitch.init();
    WorldSwitch.worldsURL = '/game.php?village=39143&screen=api&ajax=world_switch&';

        HotKeys.init();

        Connection.connect('tr32.klanlar.org', 8082, 'cec5b6e92cde', true);

        UI.Notification.enabled = true;




    });

2 个答案:

答案 0 :(得分:1)

我假设您在某种wb.Document.GetElementById事件后执行wb_DocumentCompleted。 但是,正如您在html源代码中所看到的那样,您正在排队的元素上执行了javascript,使其忙碌且无法访问。

您可能想要尝试的是排队一个元素,该元素填充在“troop_confirm_go”之后(例如“troop_confirm_train”),并且只要此元素不返回任何内容,您就可以尝试获取您正在尝试的元素再次访问。

编辑:这样的事情可以解决问题: 此函数将每隔20 ms使用密钥中指定的id对项目进行排队,持续10秒。如果没有填充,则会被取消。

public async Task WaitForElementToLoad(String key)
    {
        element = wb.Document.GetElementById(key);
        CancellationToken ct = new CancellationTokenSource(10000).Token;

        var doc = wb.Document;
        while (true)
        {
            await Task.Delay(20, ct);

            element = wb.Document.GetElementById(key);
            if (element != null)
                if (element.InnerHtml != null)
                    break;

            doc = wb.Document;
        }
        ct.ThrowIfCancellationRequested();
    }

这样调用就可以获得你的troop_confirm_go:

await WaitForElementToLoad("troop_confirm_train");
element = wb.Document.GetElementById("troop_confirm_go");

请注意,您调用它的函数也需要async

答案 1 :(得分:0)

WebBrowser对象加载正确的DOM没有问题。这完全是我的错。我无法加载正确的网址。

相关问题