public void ScheduleGET() { var driverService = PhantomJSDriverService.CreateDefaultService(); driverService.HideCommandPromptWindow = true; // Disables verbose phantomjs output IWebDriver driver = new PhantomJSDriver(driverService); //IWebDriver driver = new FirefoxDriver(); // Debug with firefox. Console.WriteLine("Logging into Office 365."); driver.Navigate().GoToUrl("https://wegmans.sharepoint.com/resources/Pages/LaborPro.aspx"); if (driver.Title.ToString() == "Sign in to your account") { IWebElement loginentry = driver.FindElement(By.XPath("//*[@id='cred_userid_inputtext']")); loginentry.SendKeys(Username); IWebElement rememberme = driver.FindElement(By.XPath("//*[@id='cred_keep_me_signed_in_checkbox']")); rememberme.Click(); } WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30)); try { wait.Until((d) => { return (d.Title.ToString().Contains("Sign In") || d.Title.ToString().Contains("My Schedule")); }); } // Sometimes it skips the second login page. catch (WebDriverTimeoutException) { driver.Quit(); throw new ScheduleGETException("Did not recieve an appropriate response from the Sharepoint server. The connection most likely timed out."); } Console.WriteLine("Logging into Sharepoint."); if (driver.Title.ToString() == "Sign In") { try { wait.Until((d) => { return (d.FindElement(By.XPath("//*[@id='passwordInput']"))); }); } catch (Exception) { driver.Quit(); throw new ScheduleGETException("Password input box did not load correctly."); } IWebElement passwordentry = driver.FindElement(By.XPath("//*[@id='passwordInput']")); password = ConvertToUnsecureString(securePwd); passwordentry.SendKeys(password); ClearPassword(); passwordentry.Submit(); } try { wait.Until((d) => { return (d.Title.ToString().Contains("Sign In") || d.Title.ToString().Contains("My Schedule")); }); } // Checks to see if the password was incorrect. catch (WebDriverTimeoutException) { driver.Quit(); throw new ScheduleGETException("Did not recieve an appropriate response from the Sharepoint server. The connection most likely timed out."); } if (driver.Title.ToString() == "Sign In") { IWebElement error = driver.FindElement(By.XPath("//*[@id='error']")); string errorString = error.Text.ToString(); if (errorString.Contains("Incorrect user ID or password")) { while (driver.Title.ToString() == "Sign In") { IWebElement usernameentry = driver.FindElement(By.XPath("//*[@id='userNameInput']")); IWebElement passwordentry = driver.FindElement(By.XPath("//*[@id='passwordInput']")); usernameentry.Clear(); passwordentry.Clear(); Console.WriteLine("You seem to have entered the wrong username or password."); GetLoginCreds(); Console.WriteLine("Trying again..."); usernameentry.SendKeys(Username); password = ConvertToUnsecureString(securePwd); passwordentry.SendKeys(password); ClearPassword(); passwordentry.Submit(); } SaveLoginCreds(); } else { Console.WriteLine("An unexpected error has occured with the webpage."); Console.WriteLine(errorString); driver.Quit(); throw new ScheduleGETException("An unexpected error has occured with the webpage."); } } Console.WriteLine("Waiting for LaborPro..."); int retries = 2; while (true) // Retry because this error can be solved by a simple page reload. { try { wait.Until((d) => { return (d.SwitchTo().Frame(0)); }); break; } // Waits for the inline frame to load. catch (WebDriverTimeoutException) { Console.WriteLine("LaborPro link's inline frame was not generated properly."); Console.WriteLine("Reloading the page..."); driver.Navigate().Refresh(); retries--; if (retries <= 0) { driver.Quit(); throw new ScheduleGETException("LaborPro link's inline frame was not generated properly."); } } } string BaseWindow = driver.CurrentWindowHandle; try { wait.Until((d) => { return (d.FindElement(By.XPath("/html/body/a"))); }); } // Waits until javascript generates the SSO link. catch (Exception) { if (driver.Title.ToString().Contains("Sign In")) // We were redirected to the sign-in page once again, so let's fill it out again... { IWebElement usernameentry = driver.FindElement(By.XPath("//*[@id='userNameInput']")); IWebElement passwordentry = driver.FindElement(By.XPath("//*[@id='passwordInput']")); usernameentry.Clear(); passwordentry.Clear(); usernameentry.SendKeys(Username); password = ConvertToUnsecureString(securePwd); passwordentry.SendKeys(password); ClearPassword(); passwordentry.Submit(); } else { driver.Quit(); throw new ScheduleGETException("LaborPro SSO Link was not generated properly."); } } IWebElement accessschedule = driver.FindElement(By.XPath("/html/body/a")); accessschedule.Click(); string popupHandle = string.Empty; ReadOnlyCollection<string> windowHandles = driver.WindowHandles; foreach (string handle in windowHandles) { if (handle != driver.CurrentWindowHandle) { popupHandle = handle; break; } } driver.SwitchTo().Window(popupHandle); Console.WriteLine("Accessing LaborPro."); try { wait.Until((d) => { return (d.Title.ToString().Contains("Welcome")); }); } catch (WebDriverTimeoutException) { throw new ScheduleGETException("Did not properly switch to LabroPro Window."); } Schedules.Add(driver.PageSource.ToString()); for (int i = 0; i < 2; i++) // Clicks "Next" and gets the schedules for the next two weeks. { driver.FindElement(By.XPath("//*[@id='pageBody']/form/table[2]/tbody/tr[2]/td/table/tbody/tr[2]/td/table/tbody/tr/td/div/table[1]/tbody/tr/td[1]/a[3]")).Click(); Schedules.Add(driver.PageSource.ToString()); } ClearPassword(); // We don't need the password anymore, so let's not keep it laying around. driver.Quit(); Console.WriteLine("Got your Schedule."); }
public void LoadData() { IWebDriver webDriver = null; State currentState = State.Ready; try { try { webDriver = new PhantomJSDriver(DriverPath); } catch (WebDriverException exc) { throw new DriverStartupException("Impossibile avviare il web driver", exc); } webDriver.Navigate().GoToUrl(LoginUrl); Timer.Wait(2); currentState = State.Login; //Eseguo login IWebElement t_username = webDriver.FindElement(By.Id("username-id")); IWebElement t_password = webDriver.FindElement(By.Id("pwd-id")); IWebElement accesso = webDriver.FindElement(By.Name("login")); t_username.SendKeys(Username); t_password.SendKeys(Password); accesso.Click(); Timer.Wait(3); webDriver.Navigate().GoToUrl(ReportUrl); Timer.Wait(3); //Verifico sia presente il bottone di logout, per essere sicuro di essere loggato try { webDriver.FindElement(By.CssSelector(".logout.btn.btn3d.tbbtn")); } catch (Exception) { throw new ApplicationException("Login in Remedy non riuscito"); } currentState = State.LoggedIn; //Inizio ricerca IWebElement ricercaAvanzata = webDriver.FindElement(By.Id("TBadvancedsearch")); IWebElement queryInput = webDriver.FindElement(By.Id("arid1005")); IWebElement avviaRicerca = webDriver.FindElement(By.Id("TBsearchsavechanges")); ricercaAvanzata.Click(); Timer.Wait(2); queryInput.SendKeys(ReportQuery); Timer.Wait(1); avviaRicerca.Click(); Timer.Wait(10); IWebElement selezionaTicket = webDriver.FindElement(By.ClassName("SelAll")); IWebElement creaReport = webDriver.FindElement(By.ClassName("Rep")); selezionaTicket.Click(); Timer.Wait(5); creaReport.Click(); Timer.Wait(5); if (webDriver.WindowHandles.Count > 1) webDriver.SwitchTo().Window(webDriver.WindowHandles.Last()); else throw new Exception("Finestra navigazione report non trovata"); IWebElement techeReportRow = webDriver.FindElement(By.XPath("//span[text()='Teche Report']")); new Actions(webDriver) .MoveToElement(techeReportRow) .DoubleClick() .Build() .Perform(); Timer.Wait(5); if (webDriver.WindowHandles.Count > 2) webDriver.SwitchTo().Window(webDriver.WindowHandles.Last()); else throw new Exception("Finestra report non trovata"); currentState = State.ReportReady; ReadOnlyCollection<IWebElement> elem = webDriver.FindElements(By.TagName("tr")); Timer.Wait(5); var tickets = elem .Select(row => row.FindElements(By.TagName("td"))) .Where(cells => cells.Count > 0) .Select(cells => new { Ticket = cells[0].Text, Stato = cells[1].Text, DataFineAtt = cells[4].Text, Componente = cells[10].Text, ApertoDa = cells[12].Text, InCaricoA = cells[13].Text, Servizio = cells[14].Text, }) .Select(x => new { ticket = new ReportCollection( Int32.Parse(x.Ticket), x.Stato, String.IsNullOrEmpty((x.DataFineAtt ?? "").Trim()) ? (DateTime?)null : DateTime.Parse(x.DataFineAtt), x.Componente, x.ApertoDa, (x.InCaricoA ?? "").Trim() == String.Empty ? null : mailingList.FindByLoginName(x.InCaricoA), x.Servizio ), IsMAC = String.IsNullOrEmpty((x.DataFineAtt ?? "").Trim()) || x.Servizio == "MAC", }); var result = ( from pair in tickets orderby pair.IsMAC ? 0 : 1 ascending, //MAC per primi pair.ticket.dataFineAtt ascending, //Prima quelli a scadenza prossima pair.ticket.stato.ToLower().Contains("assegnato") ? 1 : 0 //Non assegnati per primi select pair.ticket) .ToArray(); _tickets = new ReadOnlyCollection<ReportCollection>(result); } finally { if (webDriver != null) { //Eseguo il logout per evitare di lasciare aperta la sessione IWebElement logoutButton; switch (currentState) { case State.LoggedIn: webDriver.SwitchTo().Window(webDriver.WindowHandles[0]); logoutButton = webDriver.FindElement(By.CssSelector(".logout.btn.btn3d.tbbtn")); break; case State.ReportReady: webDriver.SwitchTo().Window(webDriver.WindowHandles[1]); logoutButton = webDriver.FindElement(By.CssSelector("[ardbn=\"btnLogout\"]")); break; default: goto LogoutComplete; //A quanto sembra non è possibile fare un semplice return nel finally } new Actions(webDriver) .MoveToElement(logoutButton) .Click() .Build() .Perform(); Timer.Wait(4); LogoutComplete: webDriver.Dispose(); } } }
/// <summary> /// Scraper statistik for data /// </summary> /// <returns>Liste af statistik-objekter</returns> public List<Politistatistik> ScrapeStatistik() { List<Politistatistik> politistatistik = new List<Politistatistik>(); try { int aarsTal = 2007; using (IWebDriver webDriver = new PhantomJSDriver()) { webDriver.Navigate().GoToUrl("http://www.politistatistik.dk/parameter.aspx?id=27"); webDriver.FindElement(By.XPath("//*[@id='geo00']/optgroup/option[8]")).Click(); webDriver.FindElement(By.XPath("//*[@id='kriminalitet01']/optgroup[2]/option[5]")).Click(); webDriver.FindElement(By.XPath("//*[@id='rightCloBaggr']/div[5]/div[3]/div[2]/input")).Click(); foreach (var aar in webDriver.FindElements(By.XPath("//*[@name='periodeYear']"))) { aar.Click(); } webDriver.FindElement(By.XPath("//*[@id='rightCol']/div[2]/div/div[3]/img")).Click(); webDriver.SwitchTo().Window(webDriver.WindowHandles.Last()); foreach (var item in webDriver.FindElements(By.ClassName("dataitem"))) { politistatistik.Add(new Politistatistik(aarsTal++, item.Text)); } } List<Politistatistik> list = politistatistik.ToList(); TraceHjaelp(new[] { "" }, list.ToString()); return list; } catch (Exception) {} return politistatistik.ToList(); }