【问题标题】:Getting around System.UnauthorizedAccessException : Access is denied绕过 System.UnauthorizedAccessException:访问被拒绝
【发布时间】:2011-01-21 00:59:38
【问题描述】:

我正在使用 Waitin RC2、WatiN-2.0.20.1089、Windows XP OS 和 IE8 以及 VS2008 和 NUnit-2.5.7.10213。我已将站点添加到受信任列表中,我有线程睡眠,我尝试过“WaitForComplete”。然而,当脚本“返回”时,我仍然遇到未经授权的访问异常。 这是我的一段代码,尽管大部分代码都在 try catch 块中,但从未捕获到异常。

公共字符串 FindAllLinks()

    {
        /*
         * This function is designed to find and use all of the links on a given page.
         * After clicking on a link it waits for 400 milliseconds on the page so the page
         * has some time to load and then the function "hits the back button" reseting 
         * to the originating page.
         * This function is not meant to be recursive.
         */

        string message = "";
        bool flag = true;

        //Get a list of all links from the browser instance
        foreach (Link link in browserInstance.Links)
        {
            System.Threading.Thread.Sleep(1000);
            Console.WriteLine(link);
            try
            {//clicking on the link to make sure it leads somewhere
                link.Click();  //If the click fails hopefull we will thrwo out of the try block and not execute the next two commands.
                //Console.WriteLine(link);
            }
            catch (Exception)
            {//OOPs we have an error let's log a message.
                message = message + "The link titled " + link + " was not found, or did not work.\n";
                flag = false;
            }

            if (flag)
            {
                System.Threading.Thread.Sleep(1000);
                //browserInstance.WaitForComplete;
                try { browserInstance.Back(); }
                catch (UnauthorizedAccessException)
                {
                    //do nothing
                }
            }//close if flag

        }//close for each

        //return the message
        return (message);

    }//Close function


    [STAThread]
    [Test]
    public void TestTitleHomePage()
    {
        bool testPassed = false;

        if (browserInstance.Title.Contains("<title>"))

        {

            string message = FindAllLinks();

            if (message == "") { testPassed = true; }

        }//close if

        else { message = "The Title was not the same."; }


        Assert.IsTrue(testPassed, message);


    }// end TestTitleHomePage

【问题讨论】:

    标签: c# internet-explorer-8 watin


    【解决方案1】:

    我试过你的代码,我也得到了异常。我想我明白会发生什么。当你第一次做Browser.Links时,你会得到当前页面的所有链接,然后你导航到另一个页面并返回到第一页,但对于WatiN来说这是一个新页面。因此,您的枚举无法工作,因为您通过第一页的链接进行枚举。

    我建议你可以做的是获取链接的所有Uri,然后在新的浏览器中逐个尝试

    IEnumerable<Uri> uris = Browser.Links.Select(l => l.Uri);
    foreach(Uri uri in Uris)
    {
       try 
       {
          using(var browser = new IE(uri))
          {
              // do nothing or detect 404, 403, etc errors
          }
    
          // no error
       }
       catch(exception)
       {
          // log error
       }
    }
    

    【讨论】:

    • 谢谢,我认为我可能需要在新浏览器中创建每个链接,但不知道该怎么做。
    • 不客气。但是我担心 try {} catch() {} 不足以检测链接是否损坏。例如,404 错误在大多数情况下不会触发任何异常,因此您需要手动检查它。与错误 500 相同,因为服务器无论如何都会提供页面。所以你应该测试页面结果。 (见watinandmore.blogspot.com/2008/04/…)。如果您喜欢我的回答,请将其标记为正确:)
    • + 1 如果@KLA 保持在同一个浏览器中,链接将会改变,但您必须获取新链接。这种新的浏览器方法应该可以解决这个问题。
    • 顺便说一句,我没有接受答案但也没有 +1 的礼仪。猜猜这是一个新手。
    猜你喜欢
    • 2014-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 2015-02-28
    • 2017-01-24
    相关资源
    最近更新 更多