touhid_xml

Selenium C# works

Feb 27th, 2018
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using OpenQA.Selenium.Chrome;
  8. //using OpenQA.Selenium.Firefox;
  9. using OpenQA.Selenium;
  10.  
  11.  
  12. using OpenQA.Selenium.Support.UI;
  13.  
  14. namespace Facebook_Monitoring_System
  15. {
  16.     class Program
  17.     {
  18.         static void Main(string[] args)
  19.         {
  20.             Console.OutputEncoding = Encoding.UTF8;
  21.             //Get User Input
  22.             //Console.WriteLine("------------- Enter Your Facebook User Name or Email or Phone -------------");
  23.             //String FbUsername = Console.ReadLine();
  24.             //Console.WriteLine("------------- Enter Your Facebook Password -------------");
  25.             //String FbPassword = Console.ReadLine();
  26.             //Console.SetCursorPosition(0, Console.CursorTop - 1);
  27.             //ClearCurrentConsoleLine();
  28.  
  29.             Console.WriteLine("------------- Enter Desired Username to get Information -------------");
  30.             String DesiredFbUser = Console.ReadLine();
  31.  
  32.             //Temp user
  33.             String FbUsername = "dev.dnj";
  34.             String FbPassword = "**********";
  35.  
  36.             //Desired user to get Information
  37.             String DesiredUserURL = String.Format("https://mbasic.facebook.com/{0}", DesiredFbUser);
  38.  
  39.             //var chromeOptions = new ChromeOptions();
  40.             //chromeOptions.AddArguments(new List<string>() { "headless" });
  41.             //var driver = new ChromeDriver(chromeOptions);
  42.  
  43.             var chrome_options = new ChromeOptions();
  44.             chrome_options.AddArgument("--disable-notifications");
  45.             // chrome_options.addargument("headless");
  46.             var driver = new ChromeDriver(chrome_options);
  47.  
  48.             // var driver = new FirefoxDriver();
  49.  
  50.  
  51.  
  52.             // Go to google
  53.             driver.Navigate().GoToUrl("https://mbasic.facebook.com/");
  54.             //IWebElement query = driver.FindElement(By.Name("q"));
  55.             // query.SendKeys("Rafusoft");
  56.  
  57.             /*******************
  58.             *  Login Page
  59.             ********************/
  60.  
  61.  
  62.             //Selectors
  63.  
  64.             //Login
  65.             var EmailInput = driver.FindElement(By.CssSelector("input[name=\"email\"]"));
  66.             var PassInput = driver.FindElement(By.CssSelector("input[name=\"pass\"]"));
  67.             var LoginButton = driver.FindElement(By.CssSelector("input[value=\"Log In\"]"));
  68.  
  69.  
  70.          
  71.             //Perform Input action and submit login
  72.             EmailInput.SendKeys(FbUsername);
  73.            // driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2);
  74.             PassInput.SendKeys(FbPassword);
  75.             //driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(2);
  76.             LoginButton.Click();
  77.             // driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
  78.  
  79.  
  80.  
  81.             if ( IsElementPresent( driver, By.CssSelector("a[href^=\"/login/save-device\"]")))
  82.             {
  83.                 var NotNowLink = driver.FindElement(By.CssSelector("a[href^=\"/login/save-device\"]"));
  84.                 NotNowLink.Click();
  85.             }
  86.  
  87.             /*************************
  88.             * Desired User Profile
  89.             ***************************/
  90.             // Go to desired user profile
  91.             driver.Navigate().GoToUrl(DesiredUserURL);
  92.  
  93.             StreamWriter sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + @"\PostData.txt", true);
  94.  
  95.             IList<IWebElement> all = driver.FindElements(By.CssSelector("div[role=\"article\"]"));
  96.             String[] allText = new String[all.Count];
  97.             int i = 0;
  98.             foreach (IWebElement element in all)
  99.             {
  100.                 allText[i++] = element.Text;
  101.  
  102.  
  103.  
  104.                 try
  105.                 {
  106.                     String PosterName = element.FindElement(By.CssSelector("div div:nth-child(1) h3")).Text;
  107.                    
  108.                    // String PostLikeCount = element.FindElement(By.XPath(".//*/a[1]")).Text;
  109.                     //Console.WriteLine(element.Text);
  110.                     sw.WriteLine(String.Format("Post Title By: {0}\n", PosterName));
  111.                  
  112.                    // sw.WriteLine(String.Format("Post Like Count: {0}\n\n", PostLikeCount));
  113.                 }
  114.                 catch(NoSuchElementException)
  115.                 {
  116.                     String PostContent = element.FindElement(By.CssSelector("div div:nth-child(2)")).Text;
  117.                     sw.WriteLine(String.Format("Post Content: {0}\n\n", PostContent));
  118.                 }
  119.  
  120.                 try
  121.                 {
  122.  
  123.                 }
  124.                 catch (NoSuchElementException)
  125.                 {
  126.  
  127.                 }
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.             }
  138.             sw.Flush();
  139.             sw.Close();
  140.  
  141.             /**
  142.             StreamWriter sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + @"\PostData.txt",true);
  143.             IReadOnlyCollection<IWebElement> FbPosts = driver.FindElements(By.CssSelector("div[role=\"article\"]"));
  144.             for (int i = 0; i < FbPosts.Count; i++)
  145.             {
  146.                 var PostInfo = FbPosts.ElementAt(i);
  147.                 String Poster = PostInfo.FindElement(By.XPath("//div[1]/div[1]/table/tbody/tr/td[2]/div/h3/strong/a")).Text;
  148.                 String PostContent = PostInfo.FindElement(By.XPath("//div[2]/div[2]/span/p")).GetAttribute("innerHTML"); ;
  149.                 Console.WriteLine(String.Format("Posted By: {0}\n", Poster));
  150.                 sw.WriteLine(String.Format("Posted By: {0}\n", Poster));
  151.                 Console.WriteLine(String.Format("Post Content: {0}\n", PostContent));
  152.                 sw.WriteLine(String.Format("Post Content: {0}\n", PostContent));
  153.  
  154.                 Console.WriteLine("\n\n\n");
  155.             }
  156.             sw.Flush();
  157.             sw.Close();
  158.             **/
  159.  
  160.  
  161.  
  162.  
  163.             /**
  164.  
  165.             try
  166.             {
  167.                 driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
  168.                 driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
  169.                 driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
  170.                 driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
  171.                 driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
  172.                 driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
  173.                 driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
  174.                 driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
  175.                 driver.FindElement(By.XPath("//a[contains(text(),'Show more')]")).Click();
  176.  
  177.             }
  178.             catch (Exception Ex)
  179.             {
  180.                 Console.WriteLine(Ex.Message);
  181.             } **/
  182.  
  183.  
  184.             int numberOfElements = driver.FindElements(By.CssSelector("div[role=\"article\"]")).Count;
  185.             Console.WriteLine(String.Format("Total Facebook Post: {0}", numberOfElements));
  186.  
  187.  
  188.             ////Log Out
  189.             //try{
  190.             //    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(3));
  191.             //    for (int i = 1; i < 6; i++)
  192.             //    {
  193.             //        By locator = By.CssSelector("a[href^=\"/logout.php\"]");
  194.             //        IWebElement element = driver.FindElement(locator);
  195.             //        element.Click();
  196.             //        wait.Until(ExpectedConditions.StalenessOf(element));
  197.             //        wait.Until(ExpectedConditions.ElementToBeClickable(locator));
  198.             //    }
  199.             //    //var LogOut = driver.FindElement(By.CssSelector("a[href^=\"/logout.php\"]"));
  200.             //    //LogOut.Click();
  201.             //}
  202.             //catch (Exception Ex)
  203.             //{
  204.             //    Console.WriteLine(Ex.Message);
  205.             //}
  206.  
  207.  
  208.  
  209.             WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(100)); // time out after 5 seconds
  210.  
  211.  
  212.             // Console.WriteLine(String.Format("Page title is: {0}",driver.));
  213.             driver.Close();
  214.             driver.Quit();
  215.             Console.ReadKey();
  216.            
  217.         }
  218.  
  219.         private static bool IsElementPresent(ChromeDriver driver, By by)
  220.         {
  221.             try
  222.             {
  223.                 driver.FindElement(by);
  224.                 return true;
  225.             }
  226.             catch (NoSuchElementException)
  227.             {
  228.                 return false;
  229.             }
  230.         }
  231.  
  232.         public static void ClearCurrentConsoleLine()
  233.         {
  234.             int currentLineCursor = Console.CursorTop;
  235.             Console.SetCursorPosition(0, Console.CursorTop);
  236.             Console.Write(new string(' ', Console.WindowWidth));
  237.             Console.SetCursorPosition(0, currentLineCursor);
  238.         }
  239.  
  240.  
  241.     }
  242. }
Add Comment
Please, Sign In to add comment