/// <summary> /// Finds web element by jQuery selector /// </summary> /// <param name="driver">Webdriver extension</param> /// <param name="selector">Defined selector</param> /// <param name="timeout">Max execution timeout in seconds. Default to 30 sec.</param> /// <param name="isEnabled">Check whether element enabled or not. Defaults to true.</param> /// <returns>Returns web element</returns> public static IWebElement TryFindElementByJquery(this IWebDriver driver, string selector, int timeout = 30, bool isEnabled = true) { if (string.IsNullOrEmpty(selector)) { throw new Exception("selector is empty"); } IWebElement res = null; bool condition; try { driver.Wait(d => { try { res = (IWebElement)d.JsExecuteJavaScript(string.Format("return $('{0}')[0]", selector)); condition = (!isEnabled) || (res.Displayed && res.Enabled); driver.CheckErrorsOnPage(); //driver.WaitForPageLoaded(); return(res != null && condition); } catch { return(false); } }, TimeSpan.FromSeconds(timeout)); } catch { return(null); } return(res); }
/// <summary> /// Finds several web elements by jQuery selector /// </summary> /// <param name="driver">Webdriver extension</param> /// <param name="selector">Defined selector</param> /// <param name="timeout">Maximum execution timeout</param> /// <returns>Returns list of web elements</returns> public static List <IWebElement> TryFindElementsByJquery(this IWebDriver driver, string selector, int timeout) { var temp = new List <IWebElement>(); if (string.IsNullOrEmpty(selector)) { throw new Exception("selector is empty"); } try { // IN FF THIS 'WAIT' RETURNS 0 -> EXCEPTION, WHILE OK IN CHROME driver.Wait(d => { try { int len = driver.JsGetElementsCount(selector); for (var i = 0; i < len; i++) { var elem = (IWebElement) driver.JsExecuteJavaScript(string.Format("return $('{0}')[{1}]", selector, i)); if (elem != null) { temp.Add(elem); } else { break; } } driver.CheckErrorsOnPage(); return(len > 0); } catch { return(false); } }, TimeSpan.FromSeconds(timeout)); } catch { return(temp); } return(temp); }
public static List <IWebElement> TryFindElementsByJs(this IWebDriver driver, string selector, int timeout = 30) { var temp = new List <IWebElement>(); if (string.IsNullOrEmpty(selector)) { throw new Exception("selector is empty"); } try { // IN FF THIS 'WAIT' RETURNS 0 -> EXCEPTION, WHILE OK IN CHROME driver.Wait(d => { try { var elements = (IList <IWebElement>)driver.JsExecuteJavaScript(string.Format("return document.querySelectorAll(\"{0}\")", selector)); if (elements != null) { temp.AddRange(elements); } driver.CheckErrorsOnPage(); return(true); } catch { return(false); } }, TimeSpan.FromSeconds(timeout)); } catch { return(temp); } return(temp); }