public static void TrimVisitedUrls() { var quarter = VisitedUrls.Count / 4; foreach (var item in VisitedUrls.Take(quarter).ToList()) { VisitedUrls.Remove(item); } Console.WriteLine("Trimming cached visited urls to 3 quarters"); }
/// <summary> /// Smooth chrome navigation. Can be stopped. /// </summary> /// <param name="URL"></param> /// <param name="delaySeconds"></param> /// <param name="tryouts"></param> /// <returns></returns> public bool Navigate(string URL, double delaySeconds, int tryouts = 1) { #region check arguments if (ChromeDriver == null) { throw new ArgumentNullException("Chrome is null"); } if (string.IsNullOrEmpty(URL)) { throw new ArgumentNullException("URL is null or empty"); } if (delaySeconds <= 0 || tryouts <= 0) { throw new ArgumentOutOfRangeException("Delay and tryouts cannot be less or equal 0"); } #endregion try { //toleratable errors counters int noInternetTimes = 0, connectionResetTimes = 0, mainFrameErrorTimes = 0, tooLongToRespondTimes = 0; var cd = ChromeDriver; for (int i = 0; i < tryouts; i++) { this.CurrentUrl = URL; VisitedUrls.Add(this.CurrentUrl); Wait.Do(delaySeconds); if (Wait.Stop) { return(false); } isExceptionalError(cd.PageSource, cd.Url); if (isContinuableError(ref noInternetTimes, ref connectionResetTimes, ref mainFrameErrorTimes, ref tooLongToRespondTimes)) { continue; } //if no errors return(true); } #region build and throw Exception message, since I failed to download the page tryouts times string message = "No connection after " + tryouts + " tryouts"; if (noInternetTimes > 0) { message += "\nNo internet connection times " + noInternetTimes; } if (connectionResetTimes > 0) { message += "\nConnection reset times " + connectionResetTimes; } if (mainFrameErrorTimes > 0) { message += "\nCommon error times " + mainFrameErrorTimes; } throw new Exception(message); #endregion } catch (Exception e) { throw new Exception("Cannot navigate chrome: " + e.Message); } }