public static void LogIntoInstagram() { var loginCredentials = CredentialsRepository.ReadCredentials(); var webDriver = WebDriverProvider.WebDriver; webDriver.Navigate().GoToUrl(new Uri("https://www.instagram.com/accounts/login/")); ThreadDelayer.WaitSomeTime(); AcceptCookies(webDriver); ThreadDelayer.WaitSomeTime(); var userName = webDriver.FindElement(By.Name("username")); var password = webDriver.FindElement(By.Name("password")); userName.InsertText(loginCredentials.Username); ThreadDelayer.WaitSomeTime(WaitingPeriod.Short); password.InsertText(loginCredentials.Password); ThreadDelayer.WaitSomeTime(WaitingPeriod.Short); var loginButton = webDriver.FindElements(By.TagName("button")).First(x => x.Text.ToLower() == "log in"); loginButton.Submit(); ThreadDelayer.WaitSomeTime(WaitingPeriod.Short); ThreadDelayer.WaitSomeTime(); }
private static void LikeSomePosts(int postsNumber) { LoadPostsInPage(); var popularPostsNumber = 9; var likesGiven = popularPostsNumber; postsNumber += popularPostsNumber; var posts = WebDriverProvider.WebDriver.FindElements(By.ClassName("_9AhH0")); try { while (likesGiven < posts.Count && likesGiven < postsNumber) { LikePost(posts[likesGiven]); ThreadDelayer.WaitSomeTime(WaitingPeriod.Short); likesGiven++; } return; } catch (StaleElementReferenceException ex) { _logger.Error("Element became unavaiable before attemot to open. Reloading elements."); posts = WebDriverProvider.WebDriver.FindElements(By.ClassName("_9AhH0")); } catch (Exception ex) { _logger.Error($"Exception of type {ex.GetType()} occured and was not handled. Exception message: {ex.Message} Exception stack trace:{ex.StackTrace}"); return; } }
public static void StopRunning() { _cancelationTokenSource.Cancel(); ThreadDelayer.WaitSomeTime(); _logger.Information("Canceled the token on runner."); WebDriverProvider.CloseWebDriver(); _logger.Information("Closed web driver"); }
public static void InsertText(this IWebElement targetElement, string inputText) { foreach (var c in inputText) { ThreadDelayer.WaitSomeTime(WaitingPeriod.VeryShort); targetElement.SendKeys(c.ToString()); } }
private static void OpenPost(IWebElement post) { try { post.Click(); ThreadDelayer.WaitSomeTime(); } catch (Exception ex) { _logger.Error($"Exception of type {ex.GetType()} occured when attempting to open a post. Exception message: {ex.Message}"); } }
private static void LoadPostsInPage() { _logger.Information("Loading posts into page."); for (int i = 0; i < 50; i++) { ((IJavaScriptExecutor)WebDriverProvider.WebDriver).ExecuteScript("scrollBy(0,1000)"); ThreadDelayer.WaitSomeTime(WaitingPeriod.Short); } ((IJavaScriptExecutor)WebDriverProvider.WebDriver).ExecuteScript("window.scrollTo(0, 0)"); ThreadDelayer.WaitSomeTime(WaitingPeriod.Short); }
private static void ClosePost() { try { var closePhotoButton = WebDriverProvider.WebDriver.FindElements(By.CssSelector("[aria-label=Close]")); closePhotoButton.First().Click(); ThreadDelayer.WaitSomeTime(WaitingPeriod.Short); } catch (Exception ex) { _logger.Error($"Exception of type {ex.GetType()} occured when attempting close post. Exception message: {ex.Message}"); } }
private static void SearchHashtag(string tag) { _logger.Information($"Searching tag {tag}."); var hashtag = tag.StartsWith("#") ? tag : "#" + tag; var searchBar = WebDriverProvider.WebDriver.FindElement(By.ClassName("XTCLo")); searchBar.InsertText(hashtag); ThreadDelayer.WaitSomeTime(); searchBar.SendKeys(Keys.Return); ThreadDelayer.WaitSomeTime(WaitingPeriod.Short); searchBar.SendKeys(Keys.Return); ThreadDelayer.WaitSomeTime(); }
private static void HandleNotificationsSetting() { try { for (int i = 0; i <= 1; i++) { _logger.Information("Handling notification settings pop-up"); var notNow = WebDriverProvider.WebDriver.FindElement(By.XPath("//button[text()=\"Not Now\"]")); notNow.Click(); ThreadDelayer.WaitSomeTime(); } } catch (Exception ex) { _logger.Error($"Exception of type {ex.GetType()} occured when attempting to handle notification settings pop-up. Exception message: {ex.Message}"); } }
private static void ClickHeart() { try { _logger.Information("Attempting to like"); var buttons = WebDriverProvider.WebDriver.FindElements(By.CssSelector("[aria-label=Like]")); var likeButton = buttons.LastOrDefault(x => x.Displayed && x.Enabled); if (likeButton != null) { likeButton.Click(); ThreadDelayer.WaitSomeTime(WaitingPeriod.Short); _logger.Information("Like given successfully"); } } catch (Exception ex) { _logger.Error($"Exception of type {ex.GetType()} occured when attempting to like. Exception message: {ex.Message}"); } finally { ThreadDelayer.WaitSomeTime(WaitingPeriod.Short); } }