Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- using OpenQA.Selenium.Chrome;
- //using OpenQA.Selenium.Firefox;
- using OpenQA.Selenium;
- using OpenQA.Selenium.Support.UI;
- namespace Facebook_Monitoring_System
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.OutputEncoding = Encoding.UTF8;
- //Get User Input
- //Console.WriteLine("------------- Enter Your Facebook User Name or Email or Phone -------------");
- //String FbUsername = Console.ReadLine();
- //Console.WriteLine("------------- Enter Your Facebook Password -------------");
- //String FbPassword = Console.ReadLine();
- //Console.SetCursorPosition(0, Console.CursorTop - 1);
- //ClearCurrentConsoleLine();
- Console.WriteLine("------------- Enter Desired Username to get Information -------------");
- String DesiredFbUser = Console.ReadLine();
- //Temp user
- String FbUsername = "dev.dnj";
- String FbPassword = "**********";
- //Desired user to get Information
- String DesiredUserURL = String.Format("https://mbasic.facebook.com/{0}", DesiredFbUser);
- //var chromeOptions = new ChromeOptions();
- //chromeOptions.AddArguments(new List<string>() { "headless" });
- //var driver = new ChromeDriver(chromeOptions);
- var chrome_options = new ChromeOptions();
- chrome_options.AddArgument("--disable-notifications");
- // chrome_options.addargument("headless");
- var driver = new ChromeDriver(chrome_options);
- // var driver = new FirefoxDriver();
- // Go to google
- driver.Navigate().GoToUrl("https://mbasic.facebook.com/");
- //IWebElement query = driver.FindElement(By.Name("q"));
- // query.SendKeys("Rafusoft");
- /*******************
- * Login Page
- ********************/
- //Selectors
- //Login
- var EmailInput = driver.FindElement(By.CssSelector("input[name=\"email\"]"));
- var PassInput = driver.FindElement(By.CssSelector("input[name=\"pass\"]"));
- var LoginButton = driver.FindElement(By.CssSelector("input[value=\"Log In\"]"));
- //Perform Input action and submit login
- EmailInput.SendKeys(FbUsername);
- // driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2);
- PassInput.SendKeys(FbPassword);
- //driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2);
- LoginButton.Click();
- // driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
- if ( IsElementPresent( driver, By.CssSelector("a[href^=\"/login/save-device\"]")))
- {
- var NotNowLink = driver.FindElement(By.CssSelector("a[href^=\"/login/save-device\"]"));
- NotNowLink.Click();
- }
- /*************************
- * Desired User Profile
- ***************************/
- // Go to desired user profile
- driver.Navigate().GoToUrl(DesiredUserURL);
- StreamWriter sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + @"\PostData.txt", true);
- IList<IWebElement> all = driver.FindElements(By.CssSelector("div[role=\"article\"]"));
- String[] allText = new String[all.Count];
- int i = 0;
- foreach (IWebElement element in all)
- {
- allText[i++] = element.Text;
- try
- {
- String PosterName = element.FindElement(By.CssSelector("div div:nth-child(1) h3")).Text;
- // String PostLikeCount = element.FindElement(By.XPath(".//*/a[1]")).Text;
- //Console.WriteLine(element.Text);
- sw.WriteLine(String.Format("Post Title By: {0}\n", PosterName));
- // sw.WriteLine(String.Format("Post Like Count: {0}\n\n", PostLikeCount));
- }
- catch(NoSuchElementException)
- {
- String PostContent = element.FindElement(By.CssSelector("div div:nth-child(2)")).Text;
- sw.WriteLine(String.Format("Post Content: {0}\n\n", PostContent));
- }
- try
- {
- }
- catch (NoSuchElementException)
- {
- }
- }
- sw.Flush();
- sw.Close();
- /**
- StreamWriter sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + @"\PostData.txt",true);
- IReadOnlyCollection<IWebElement> FbPosts = driver.FindElements(By.CssSelector("div[role=\"article\"]"));
- for (int i = 0; i < FbPosts.Count; i++)
- {
- var PostInfo = FbPosts.ElementAt(i);
- String Poster = PostInfo.FindElement(By.XPath("//div[1]/div[1]/table/tbody/tr/td[2]/div/h3/strong/a")).Text;
- String PostContent = PostInfo.FindElement(By.XPath("//div[2]/div[2]/span/p")).GetAttribute("innerHTML"); ;
- Console.WriteLine(String.Format("Posted By: {0}\n", Poster));
- sw.WriteLine(String.Format("Posted By: {0}\n", Poster));
- Console.WriteLine(String.Format("Post Content: {0}\n", PostContent));
- sw.WriteLine(String.Format("Post Content: {0}\n", PostContent));
- Console.WriteLine("\n\n\n");
- }
- sw.Flush();
- sw.Close();
- **/
- /**
- try
- {
- driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
- driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
- driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
- driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
- driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
- driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
- driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
- driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
- driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
- }
- catch (Exception Ex)
- {
- Console.WriteLine(Ex.Message);
- } **/
- int numberOfElements = driver.FindElements(By.CssSelector("div[role=\"article\"]")).Count;
- Console.WriteLine(String.Format("Total Facebook Post: {0}", numberOfElements));
- ////Log Out
- //try{
- // WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(3));
- // for (int i = 1; i < 6; i++)
- // {
- // By locator = By.CssSelector("a[href^=\"/logout.php\"]");
- // IWebElement element = driver.FindElement(locator);
- // element.Click();
- // wait.Until(ExpectedConditions.StalenessOf(element));
- // wait.Until(ExpectedConditions.ElementToBeClickable(locator));
- // }
- // //var LogOut = driver.FindElement(By.CssSelector("a[href^=\"/logout.php\"]"));
- // //LogOut.Click();
- //}
- //catch (Exception Ex)
- //{
- // Console.WriteLine(Ex.Message);
- //}
- WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(100)); // time out after 5 seconds
- // Console.WriteLine(String.Format("Page title is: {0}",driver.));
- driver.Close();
- driver.Quit();
- Console.ReadKey();
- }
- private static bool IsElementPresent(ChromeDriver driver, By by)
- {
- try
- {
- driver.FindElement(by);
- return true;
- }
- catch (NoSuchElementException)
- {
- return false;
- }
- }
- public static void ClearCurrentConsoleLine()
- {
- int currentLineCursor = Console.CursorTop;
- Console.SetCursorPosition(0, Console.CursorTop);
- Console.Write(new string(' ', Console.WindowWidth));
- Console.SetCursorPosition(0, currentLineCursor);
- }
- }
- }
Add Comment
Please, Sign In to add comment