解决 System.UnauthorizedAccessException :访问被拒绝

发布于 2024-10-12 19:13:36 字数 2299 浏览 2 评论 0原文

我正在使用 Waitin RC2、WatiN-2.0.20.1089、Windows XP 操作系统、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

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()

    {
        /*
         * 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生来就爱笑 2024-10-19 19:13:36

我尝试了你的代码,我也得到了例外。我想我明白发生了什么。当你第一次执行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
   }
}

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.

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
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文