WebBrowser ID Access - not in DocumentText but in

2019-09-03 08:02发布

I am using the WebBrowser object, for web automation purposes on a windows form .Net Framework 4.5 application. In a certain page, I am not able to see the id in my WebBrowser.DocumentText even though I can see it when I open View page source from an actual browser.

This is the code that I always use to get the HtmlElement inside a page.

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

To sum up how can I retrieve an ID that is not inside DocumentText but inside the page source ?

Edit: This is how it looks like in the Page Source data. Just press conrol + f and search for the id named troop_confirm_go:

<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条回答
手持菜刀,她持情操
2楼-- · 2019-09-03 08:18

I am assuming you execute your wb.Document.GetElementById after some kind of wb_DocumentCompleted event. However, as you can see in your html source, there is javascript being executed on the element you are queuing for, making it busy and inaccessible by that time.

What you might want to try is queuing for an element, which is populated after "troop_confirm_go" (e.g. "troop_confirm_train") and as soon as this element does not return nothing, you can try and get the element you're trying to access again.

EDIT: Something like this should do the trick: This function queues for the item with the id specified in key every 20 ms for 10 seconds. If it is not populated it is cancelled.

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();
    }

Calling it like this should get you your troop_confirm_go:

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

Note that the function you call this from needs to be async too.

查看更多
时光不老,我们不散
3楼-- · 2019-09-03 08:20

The WebBrowser object has no problem loading the correct DOM. Its totally my fault. I was not able to load the correct URL.

查看更多
登录 后发表回答