2017-07-04 42 views
0

我有一個網頁瀏覽器導航到這個網址:https://www.tinglysning.dk/m/#/soegC#的Web瀏覽器中調用點擊

我想通過自動輸入文本到文本框中自動對搜索,然後點擊「SOG」按鈕。我做這個:

 webBrowser1.Document.GetElementById("adresse").InnerText = "hobrovej 35 9000"; 
     webBrowser1.Document.GetElementById("btn-soeg").InvokeMember("Click"); 

所有這一切工作正常,問題是,當點擊該按鈕時,那隻能說明一個裝載箱,而不是實際結果提前

感謝

回答

0

問題是,WebBrowser控件有很多安全限制,如果單頁面應用程序(SPA)(如使用AJAX)阻止正確執行。 https://www.tinglysning.dk/m/#/soeg似乎是一個Angular應用程序。

我假設你開發了一個WindowsFormsApplication(因爲WPF WebBrowser沒有GetElementById函數)。我發現一個工作解決方案,在這篇文章中的答案:https://stackoverflow.com/a/18333982/1681616

下面的代碼會禁用這些安全限制,並允許Angular應用程序正常工作。在初始化WebBrowser控件之前,請確保致電SetBrowserFeatureControl。例如。在表格之前調用此方法InitializeComponent

private void SetBrowserFeatureControl() 
{ 
    // http://msdn.microsoft.com/en-us/library/ee330720(v=vs.85).aspx 

    // FeatureControl settings are per-process 
    var fileName = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName); 

    // make the control is not running inside Visual Studio Designer 
    if (string.Compare(fileName, "devenv.exe", true) == 0 
     || string.Compare(fileName, "XDesProc.exe", true) == 0) 
     return; 

    SetBrowserFeatureControlKey("FEATURE_BROWSER_EMULATION", 
           fileName, 
           GetBrowserEmulationMode()); // Webpages containing standards-based !DOCTYPE directives are displayed in IE10 Standards mode. 
    SetBrowserFeatureControlKey("FEATURE_AJAX_CONNECTIONEVENTS", fileName, 1); 
    SetBrowserFeatureControlKey("FEATURE_ENABLE_CLIPCHILDREN_OPTIMIZATION", fileName, 1); 
    SetBrowserFeatureControlKey("FEATURE_MANAGE_SCRIPT_CIRCULAR_REFS", fileName, 1); 
    SetBrowserFeatureControlKey("FEATURE_DOMSTORAGE ", fileName, 1); 
    SetBrowserFeatureControlKey("FEATURE_GPU_RENDERING ", fileName, 1); 
    SetBrowserFeatureControlKey("FEATURE_IVIEWOBJECTDRAW_DMLT9_WITH_GDI ", fileName, 0); 
    SetBrowserFeatureControlKey("FEATURE_DISABLE_LEGACY_COMPRESSION", fileName, 1); 
    SetBrowserFeatureControlKey("FEATURE_LOCALMACHINE_LOCKDOWN", fileName, 0); 
    SetBrowserFeatureControlKey("FEATURE_BLOCK_LMZ_OBJECT", fileName, 0); 
    SetBrowserFeatureControlKey("FEATURE_BLOCK_LMZ_SCRIPT", fileName, 0); 
    SetBrowserFeatureControlKey("FEATURE_DISABLE_NAVIGATION_SOUNDS", fileName, 1); 
    SetBrowserFeatureControlKey("FEATURE_SCRIPTURL_MITIGATION", fileName, 1); 
    SetBrowserFeatureControlKey("FEATURE_SPELLCHECKING", fileName, 0); 
    SetBrowserFeatureControlKey("FEATURE_STATUS_BAR_THROTTLING", fileName, 1); 
    SetBrowserFeatureControlKey("FEATURE_TABBED_BROWSING", fileName, 1); 
    SetBrowserFeatureControlKey("FEATURE_VALIDATE_NAVIGATE_URL", fileName, 1); 
    SetBrowserFeatureControlKey("FEATURE_WEBOC_DOCUMENT_ZOOM", fileName, 1); 
    SetBrowserFeatureControlKey("FEATURE_WEBOC_POPUPMANAGEMENT", fileName, 0); 
    SetBrowserFeatureControlKey("FEATURE_WEBOC_MOVESIZECHILD", fileName, 1); 
    SetBrowserFeatureControlKey("FEATURE_ADDON_MANAGEMENT", fileName, 0); 
    SetBrowserFeatureControlKey("FEATURE_WEBSOCKET", fileName, 1); 
    SetBrowserFeatureControlKey("FEATURE_WINDOW_RESTRICTIONS ", fileName, 0); 
    SetBrowserFeatureControlKey("FEATURE_XMLHTTP", fileName, 1); 
} 

private void SetBrowserFeatureControlKey(string feature, 
              string appName, 
              uint value) 
{ 
    using (var key = Registry.CurrentUser.CreateSubKey(
                 string.Concat(@"Software\Microsoft\Internet Explorer\Main\FeatureControl\", feature), 
                 RegistryKeyPermissionCheck.ReadWriteSubTree)) 
    { 
     key.SetValue(appName, (uint) value, RegistryValueKind.DWord); 
    } 
} 

private uint GetBrowserEmulationMode() 
{ 
    var browserVersion = 7; 
    using (var ieKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer", 
                 RegistryKeyPermissionCheck.ReadSubTree, 
                 RegistryRights.QueryValues)) 
    { 
     var version = ieKey.GetValue("svcVersion"); 
     if (null == version) 
     { 
      version = ieKey.GetValue("Version"); 
      if (null == version) 
       throw new ApplicationException("Microsoft Internet Explorer is required!"); 
     } 

     int.TryParse(version.ToString().Split('.')[0], out browserVersion); 
    } 

    uint 
     mode = 11000; // Internet Explorer 11. Webpages containing standards-based !DOCTYPE directives are displayed in IE11 Standards mode. Default value for Internet Explorer 11. 
    switch (browserVersion) 
    { 
     case 7: 
      mode = 7000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode. Default value for applications hosting the WebBrowser Control. 
      break; 
     case 8: 
      mode = 8000; // Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode. Default value for Internet Explorer 8 
      break; 
     case 9: 
      mode = 9000; // Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode. Default value for Internet Explorer 9. 
      break; 
     case 10: 
      mode = 10000; // Internet Explorer 10. Webpages containing standards-based !DOCTYPE directives are displayed in IE10 mode. Default value for Internet Explorer 10. 
      break; 
     default: 
      // use IE11 mode by default 
      break; 
    } 

    return mode; 
}