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);
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.