You could refactor it into a query, making the intentions of the code somewhat clearer:
var nextElement = tableElement.All
.Where(element => element.GetAttribute("id") != null &&
element.GetAttribute("id").Contains("catalogEntry_img") &&
!visitedUrls.Contains(element.GetAttribute("href")))
.FirstOrDefault();
if(nextElement != null)
{
visitedUrls.Add(nextElement.GetAttribute("href"));
nextElement.InvokeMember("Click");
currentUrl = nextElement.GetAttribute("href");
}
I'd also suggest changing visitedUrls
to a HashSet
, rather than a list, as it is a more efficient data structure for simply determining if an item is in a set of items.