绕过System.UnauthorizedAccessException:访问被拒绝 [英] Getting around System.UnauthorizedAccessException : Access is denied

查看:322
本文介绍了绕过System.UnauthorizedAccessException:访问被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Waitin RC2,WatiN-2.0.20.1089,Windows XP操作系统,IE8和VS2008以及NUnit-2.5.7.10213。我已将网站添加到可信列表,我有线程睡眠,我尝试过WaitForComplete。但当脚本退回时,我仍然会收到未经授权的访问异常。
这是我的一大块代码,尽管大多数代码都在try catch块中,但是从来没有捕获异常。

I am using Waitin RC2, WatiN-2.0.20.1089, Windows XP OS with IE8 With VS2008 and NUnit-2.5.7.10213. I have added the sites to the trusted list, I have thread sleeps, I have tried "WaitForComplete". yet when the script goes "back" I am still getting an unauthorized access exception. Here is a chunk of my code, the exceptions are never caught inspite of the fact that most of the code is in try catch blocks.

public string FindAllLinks ()

public string 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


推荐答案

我尝试了你的代码,我也得到了例外。我想我明白会发生什么。当您第一次执行 Browser.Links 时,您将获得当前页面的所有链接,然后您导航到另一个页面并返回到第一页,但对于WatiN,它是一个新一页。因此,您的枚举无效,因为您枚举了第一页的链接。

I tried your code and I also get the exception. I think I understand what happens. When you first do Browser.Links, you get all the links of the current page, then you navigate to another page and return to the first page, but for WatiN it is a new page. So your enumeration cannot work because you enumerate though the links of the first page.

我建议您可以做的是获取所有链接的Uri,然后尝试它们在新浏览器中一个接一个地

What I suggest you could do is to get all the Uri of the links, then try them one by one in a new browser

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
   }
}

这篇关于绕过System.UnauthorizedAccessException:访问被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆