public static List <SearchedResult> SearchText(string keyWord) { List <SearchedResult> searchedResults = new List <SearchedResult>(); var element = GraphBrowser.FindElement(By.CssSelector("input#q")); element.Clear(); element.SendKeys(keyWord); var searchButton = GraphBrowser.FindElement(By.XPath("//button[text()='Search']")); GraphBrowser.Click(searchButton); GraphBrowser.Wait(By.CssSelector("ul#local-docs-ul>li")); int resultCount = GraphBrowser.webDriver.FindElements(By.CssSelector("ul#local-docs-ul>li")).Count; for (int i = 0; i < resultCount; i++) { SearchedResult result = new SearchedResult(); result.Name = GraphBrowser.webDriver.FindElement(By.CssSelector("ul#local-docs-ul>li:nth-child(" + (int)(i + 2) + ")>div > div.event-info > div > div.col-xs-8.name.cp1")).Text; result.Description = GraphBrowser.webDriver.FindElement(By.CssSelector("ul#local-docs-ul>li:nth-child(" + (int)(i + 2) + ")> div > div> div.desc")).Text; result.DetailLink = GraphBrowser.webDriver.FindElement(By.CssSelector("ul#local-docs-ul>li:nth-child(" + (int)(i + 2) + ") > div > div.event-info > div > div.col-xs-8.event-links > a")).GetAttribute("href"); searchedResults.Add(result); } return(searchedResults); }
/// <summary> /// Returns the filtered traings /// </summary> /// <param name="searchString">The search text to use</param> /// <returns>The search result list. Each result contains the training title and description</returns> public static List <SearchedResult> GetFilterResults(string searchString = "") { InputSearchString(searchString); List <SearchedResult> resultList = new List <SearchedResult>(); var nextElement = Browser.FindElement(By.ClassName("next-link")); bool shouldReadFirstPage = true; do { if (shouldReadFirstPage) { shouldReadFirstPage = false; } else { Browser.Click(nextElement); } var uList = Browser.FindElement(By.CssSelector(@"#OrderedResults+ul")); IReadOnlyList <IWebElement> listItems = uList.FindElements(By.XPath("li")); for (int i = 0; i < listItems.Count; i++) { SearchedResult resultInfo = new SearchedResult(); resultInfo.Name = listItems[i].GetAttribute("aria-label"); var descriptionElement = listItems[i].FindElement(By.ClassName("description")); resultInfo.Description = descriptionElement.Text; resultInfo.ViewCount = Convert.ToInt64((listItems[i].FindElement(By.XPath("//span[contains(text(),' views')]")).GetAttribute("innerHTML").Split(' '))[0]); // Add if() here to reduce the time cost searching for non-existent element of class date-updated if (!Browser.Url.Contains("/training")) { IWebElement updatedDateElement; try { updatedDateElement = listItems[i].FindElement(By.CssSelector(".date-updated")); } catch (NoSuchElementException) { updatedDateElement = null; } if (updatedDateElement != null) { resultInfo.UpdatedDate = DateTime.Parse(updatedDateElement.Text.Replace("Updated ", null)); } } resultInfo.DetailLink = listItems[i].FindElement(By.XPath("//a[@role='link']")).GetAttribute("href"); resultList.Add(resultInfo); } } while (nextElement.Displayed); return(resultList); }