/// <summary> /// Freezes the current thread until at least one element that matches the query appears in the document. /// </summary> /// <param name="doc"></param> /// <param name="query"></param> /// <param name="timeout"></param> /// <returns>Matched elements.</returns> public static IEnumerable <Element> WaitSelector(this HtmlDocument doc, string query, int timeout = 0) { if (timeout == 0) { timeout = DefaultTimeout; } var selector = new CssSelector(query); var timespan = 100; for (int i = 0; i < timeout / timespan; i++) { try { var elt = selector.Select(doc).ToListOrNull(); if (elt != null) { return(elt); } } catch { } Thread.Sleep(timespan); } return(selector.Select(doc)); }
public void Select(string html, string selector, string expectedIds) { var doc = new Document(); doc.Write(html); var cssSelector = new CssSelector(selector); var items = cssSelector.Select(doc.DocumentElement).Select(x => x.Id).ToArray(); CollectionAssert.AreEqual(string.IsNullOrEmpty(expectedIds) ? new string[0] : expectedIds.Split(','), items); }