-->

四处逛逛System.UnauthorizedAccessException的:拒绝访问。(Gett

2019-09-27 10:50发布

我用的一直等着RC2,华廷-2.0.20.1089中,Windows XP操作系统与IE8使用VS2008和NUnit-2.5.7.10213。 我已经加入了网站的信任列表中,我有螺纹睡觉,我已经尝试了“WaitForComplete”。 但剧本去当“背”我仍然得到一个未经授权的访问异常。 这里是我的代码块,该异常没有抓到的事实,即大部分的代码是在尝试catch块inspite。

公共字符串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

Answer 1:

我想你的代码,我也得到了异常。 我想我明白发生了什么。 当你第一次做Browser.Links ,你当前页面的所有链接,那么你导航到另一个页面,并返回到第一页,但对于华廷它是一个新的页面。 所以,因为你列举虽然在第一页的链接你的枚举不能工作。

我建议你可以做的是获得链接的所有开放的,那么在新的浏览器尝试逐一

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


文章来源: Getting around System.UnauthorizedAccessException : Access is denied