2016-03-08 57 views
0

我使用的是WebBrowser對象,用於在Windows窗體.Net Framework 4.5應用程序上進行Web自動化操作。在某個頁面中,即使我可以在實際瀏覽器中打開查看頁面源時看到我的WebBrowser.DocumentText中的ID也無法看到。WebBrowser ID訪問 - 不在DocumentText中,但在頁面源

這是我一直用來在頁面內獲取HtmlElement的代碼。

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

總結如何檢索一個不在DocumentText內但在頁面源內部的ID?

編輯:這就是它在數據中的樣子。只需按下conrol + 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; 




    }); 

+0

你的標記是什麼樣的?你可以在你的問題中包含更多? –

+0

@ZachM。添加了HTML頁面源代碼 – 0014

+0

爲什麼不使用javascript將該值寫入隱藏屬性,然後您就可以抓取 –

回答

1

我假設你的wb.Document.GetElementById後某種wb_DocumentCompleted事件的執行。 但是,正如您在html源代碼中所看到的那樣,您正在排隊等待的元素上正在執行JavaScript,因此到時候這個元素很繁忙併且無法訪問。

你可能想要嘗試的是在「troop_confirm_go」之後填充的元素(例如「troop_confirm_train」),並且只要此元素不返回任何內容,就可以嘗試獲取元素試圖再次訪問。

編輯:像這樣的東西應該做的伎倆: 與關鍵每20毫秒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了。

+0

感謝您的輸入。是的,你的假設是正確的。所以如果我沒有弄錯你的意思是我應該在'DocumentCompleted'事件裏面嘗試這樣的事情; var confirm = _wb.Document.GetElementById(「troop_confirm_train」); if(confirm == null)return;那是對的嗎 ? – 0014

+0

您需要保持排隊,直到項目被填充。請看我的答案中的編輯。 – Lesic

+0

嗯,我看到了,因爲它是不尋常的,它將很難將解決方案實施到我自己的系統中。至少我會知道是什麼原因導致了這個問題。我目前正在使用你的代碼來看它是否會起作用。我會告訴你。謝謝。 – 0014

0

WebBrowser對象加載正確的DOM沒有問題。這完全是我的錯。我無法加載正確的網址。

相關問題