C# HtmlElementCollection "Access is denied" error message on second iteration of loop

0

I'm using a loop to loop through a search engine page to gather data, and for some reason when I navigate to the second page of results my HtmlElementCollection and Regex.Match functions poop out with this error:

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

Here's a copy of my loop code. Can you advise me on how to proceed and what might be causing the issue?

while (((WebBrowser)browser).Document.GetElementById("pg-next")!=null)
{
    //MessageBox.Show("hello"); 
    HtmlElementCollection col = default(HtmlElementCollection);
    col = ((WebBrowser)browser).Document.GetElementsByTagName("a");
    foreach (HtmlElement e in col)
    {
        match = Regex.Match(e.GetAttribute("href").ToString(), @"mysite.com", RegexOptions.IgnoreCase);
        if (match.Success)
        {                           
            this_url = e.GetAttribute("href").ToString();
            //MessageBox.Show(this_url);
            match = Regex.Match(this_url, @"mysite.com", RegexOptions.IgnoreCase);
            this_url = "https://"+match;
            //domorestuff
        } 
        if ((e.GetAttribute("innerHTML").ToString().Contains("Next ")))
        {
            f_perform_operation_on_element (e, "click", null);
            f_sleep(2);
        } 
    }
}

My only current lead, and it could completely wrong, is that the htmlelementcollection is expecting to have the previous pages elements but since I am declaring a brand new collection something is getting messed up. I'm really confused.

Access is denied at :

 match = Regex.Match(e.GetAttribute("href").ToString(), @"mysite.com", RegexOptions.IgnoreCase);
c#
asked on Stack Overflow Nov 11, 2011 by atwellpub • edited Nov 11, 2011 by Toomai

3 Answers

1

I think it's the e.GetAttribute that is throwing the exception...

two things...first, make sure you are running the application under a full trust account like administrator and second, consider using htmlagilitypack to do things on the html structure:

http://htmlagilitypack.codeplex.com/

answered on Stack Overflow Nov 12, 2011 by Timmerz
0

My answer was, notice the loop has an element that it clicks if it happens to be the next button. Well the loop continues but a new page has been loaded, and this sudden page-change within a loop of page elements caused the error. So I just stopped the loop after the clickoff.

answered on Stack Overflow Nov 17, 2011 by atwellpub
0

The reason this error is happening is that some of the HTML elements that are being looped do not contain the requested attribute in the code.

For example, you may loop through FONT tags in some HTML code and check if each one's COLOR attribute contains BLUE. The error will happen as soon as the loop hits an element that does not contain the requested attribute (BLUE).

To bypass this, you can try these two solutions:

  1. Check if the HTML element that is being looped contains the attribute before requesting it;
  2. Simply put the line (or the If statement) in a Try statement. This will prevent software crashing and will skip the element that caused an error and continue looping the next one.

I strongly suggest the first solution.

answered on Stack Overflow Apr 4, 2021 by Stefan Đorđević

User contributions licensed under CC BY-SA 3.0