【问题标题】:disable IE visibility while using WatiN使用 WatiN 时禁用 IE 可见性
【发布时间】:2026-01-03 22:20:07
【问题描述】:

我使用watin,因为我需要在后台打开一些用户需要支持Javascript的网站。我不知道 WatiN 是否最适合这项工作,但目前需要很长时间才能看到 Internet Explorer。我需要在使用 WatiN 时禁用弹出 Internet Explorer。用户不需要看到网站的开放。是否可以在使用 WatiN 访问网站而不向用户显示时,或者我应该使用另一种在客户端支持 JS 的替代方案? 我现在的代码;

    public static void visitURL()
       {
           IE iehandler = new IE("http://www.isjavascriptenabled.com");

           if (iehandler.ContainsText("Yes"))
               Console.WriteLine("js on");
           else
               Console.WriteLine("js off");
       }

【问题讨论】:

  • 的目标是从另一个站点/url 获取数据还是为了您自己的目的测试 javascript 的可用性?如果是之前 - 请参阅此问题/答案 *.com/questions/21255725/…,如果是后者 - 考虑包含“noscript”内容,告知用户您的应用程序需要 JS
  • 我只需要访问网页不多。在这些网站上有一些分析脚本,如谷歌分析或 piwiik。我只需要能够运行本地 js 以便他们计算我的访问次数。
  • 查看这个答案 - 你有一些选项可以从 c# *.com/questions/10161413/…生成无头浏览器

标签: c# browser watin


【解决方案1】:

WatIn.Core.IE 类有一个 Visible 属性,你可以这样初始化对象:
new WatiN.Core.IE() { Visible = true }

这样 IE 在创建时只会在屏幕上闪烁,然后会被隐藏。您可以稍后使用 WatiN.Core.IE 类的 ShowWindow 方法控制 IE 的可见性 - 我的意思是,如果需要,您可以在屏幕上显示它,或者您可以再次隐藏。

【讨论】:

    【解决方案2】:

    我正是使用这个技巧(隐藏 IE)来编写在隐藏的 IE 窗口中运行的单元测试(使用 https://github.com/o2platform/FluentSharp_Fork.WatiN

    例如here is how我创建了一个辅助类(具有可配置的隐藏值)

        public IE_TeamMentor(string webRoot, string path_XmlLibraries, Uri siteUri, bool startHidden)
        {
            this.ie                = "Test_IE_TeamMentor".popupWindow(1000,700,startHidden).add_IE();            
            this.path_XmlLibraries = path_XmlLibraries;
            this.webRoot           = webRoot;
            this.siteUri           = siteUri;
        }
    

    然后被this test消费:

       [Test] public void View_Markdown_Article__Edit__Save()
        {
            var article          = tmProxy.editor_Assert()                       // assert the editor user (or the calls below will fail due to security demands)
                                          .library_New_Article_New()             // create new article
                                          .assert_Not_Null(); 
    
            var ieTeamMentor     = this.new_IE_TeamMentor_Hidden();
            var ie               = ieTeamMentor.ie; 
            ieTeamMentor.login_Default_Admin_Account("/article/{0}".format(article.Metadata.Id));               // Login as admin and redirect to article page
    
            var original_Content = ie.element("guidanceItem").innerText().assert_Not_Null();                    // get reference to current content
            ie.assert_Has_Link("Markdown Editor")       
              .link           ("Markdown Editor").click();                                                      // open markdown editor page          
    
            ie.wait_For_Element_InnerHtml("Content").assert_Not_Null()
              .element                   ("Content").innerHtml()
                                                    .assert_Is(original_Content);                               // confirm content matches what was on the view page
    
            var new_Content      = "This is the new content of this article".add_5_RandomLetters();             // new 'test content'
    
            ie.element("Content").to_Field().value(new_Content);                                                // put new content in markdown editor
            ie.button("Save").click();                                                                          // save 
            ie.wait_For_Element_InnerHtml("guidanceItem").assert_Not_Null()        
              .element                   ("guidanceItem").innerHtml()
                                                         .assert_Is("<P>{0}</P>".format(new_Content));         // confirm that 'test content' was saved ok (and was markdown transformed)            
    
            ieTeamMentor.close();
        }
    

    这里有一些帖子可能会帮助您了解我如何使用它:

    【讨论】: