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);
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:
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.
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:
I strongly suggest the first solution.
User contributions licensed under CC BY-SA 3.0