Advertisement
kilya

Gmail-Automation_Source-Code

Jan 7th, 2019
2,216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.50 KB | None | 0 0
  1. using System;
  2. using System.Windows.Forms;
  3. using OpenQA.Selenium;
  4. using OpenQA.Selenium.Support.UI;
  5. using OpenQA.Selenium.Chrome;
  6. using System.Threading;
  7. using System.Text.RegularExpressions;
  8. using Keys = System.Windows.Forms.Keys;
  9.  
  10. namespace Gmail_Scrape
  11. {
  12.     public partial class frm_Gmail : Form
  13.     {
  14.         public frm_Gmail()
  15.         {
  16.             InitializeComponent();
  17.         }
  18.  
  19.         private void frm_Gmail_KeyUp(object sender, KeyEventArgs e)
  20.         {
  21.             //// Enable & Disable Button
  22.             //if (txtBox_User.TextLength > 5 && txtBox_Pass.TextLength > 2)
  23.             //{
  24.             //    btn_Login.Enabled = true;
  25.             //    btn_Delete.Enabled = true;
  26.             //}
  27.             //else
  28.             //{
  29.             //    btn_Login.Enabled = false;
  30.             //    btn_Delete.Enabled = false;
  31.             //}
  32.         }
  33.  
  34.         private void btn_Login_Click(object sender, EventArgs e)
  35.         {
  36.             this.Enabled = false;
  37.             this.Cursor = Cursors.WaitCursor;
  38.  
  39.             try
  40.             {
  41.                 // Declare Url of Gmail
  42.                 string UrlGmail = "https://mail.google.com/mail/";
  43.  
  44.                 // email & node email
  45.                 string email = txtBox_User.Text;
  46.                 string Node_email = "//*[@type='email']";
  47.  
  48.                 // password & node password
  49.                 string pass = txtBox_Pass.Text;
  50.                 string Node_pass = "//*[@type='password']";
  51.  
  52.                 // Nodes of Button Next
  53.                 string email_Next = "//*[@id='identifierNext']";
  54.                 string pass_Next = "//*[@id='passwordNext']";
  55.  
  56.                 // Create the reference for our browser
  57.                 IWebDriver driver = new ChromeDriver(); // path of chrome driver is Optional
  58.                 WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
  59.  
  60.                 // Navigate to Gmail.com
  61.                 driver.Navigate().GoToUrl(UrlGmail);
  62.  
  63.                 // Maximize Navigator
  64.                 driver.Manage().Window.Maximize();
  65.  
  66.                 // Implicitly Wait
  67.                 //driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);
  68.                 //driver.Manage().Timeouts().ImplicitWait(TimeSpan.FromSeconds(20));
  69.  
  70.                 // Enter Email and click next button
  71.                 driver.FindElement(By.XPath(Node_email)).SendKeys(email);
  72.                 driver.FindElement(By.XPath(email_Next)).Click();
  73.  
  74.                 Thread.Sleep(5000); // Wait 4 milisecond
  75.                 //driver.Manage().Timeouts().PageLoadTimeout(TimeSpan.FromSeconds(40));
  76.                 //wait.Until(ExpectedConditions.ElementIsVisible(By.XPath(Node_pass)));
  77.  
  78.                 // Enter Password and click next button
  79.                 driver.FindElement(By.XPath(Node_pass)).SendKeys(pass);
  80.                 driver.FindElement(By.XPath(pass_Next)).Click();
  81.  
  82.                 Thread.Sleep(5000); // Wait 4 milisecond
  83.  
  84.                 // Click to Social Media Messages
  85.                 string value_SM = "Messages provenant de réseaux sociaux, de sites de partage de fichiers multimédia, de sites de rencontre en ligne et autres sites Web sociaux.";
  86.                 driver.FindElement(By.XPath("//*[@data-tooltip='" + value_SM + "']")).Click();
  87.  
  88.                 // Node of button delete
  89.                 string node_Delet = "//div[@class='nH aqK']/div/div/div/div[2]/div[3]";
  90.  
  91.                 // Node of Selectionner Tout
  92.                 string SelectAll = "//*[@aria-label='Sélectionner']/div[1]/span";
  93.  
  94.                 // see if messages SM exit or not
  95.                 IWebElement slide = driver.FindElement(By.XPath("//*[@class='ar5 J-J5-Ji']"));
  96.                 string vide = slide.Text;
  97.                 if (vide != " ")
  98.                 {
  99.                     // Declare number of Messages in page
  100.                     string node_NbrOfmessage = "//div[@aria-label='Afficher plus de messages']/span/span[1]/span[2]";
  101.                     IWebElement nbrValue = driver.FindElement(By.XPath(node_NbrOfmessage));
  102.  
  103.                     // Declare number of pages Messages
  104.                     string node_NbrOfPages = "//div[@aria-label='Afficher plus de messages']/span/span[2]";
  105.                     IWebElement nbrOfPage = driver.FindElement(By.XPath(node_NbrOfPages));
  106.  
  107.                     // Start Conversion
  108.                     // convert number of messages in page to integer
  109.                     string nbr_Messages = nbrValue.Text;
  110.                     int j = Convert.ToInt32(nbr_Messages);
  111.  
  112.                     // convert number of pages messages to integer
  113.                     string nbr_Pages = nbrOfPage.Text;
  114.                     // Replace space
  115.                     nbr_Pages = Regex.Replace(nbr_Pages, @"\s", "");
  116.                     int h = Convert.ToInt32(nbr_Pages);
  117.                     // End Conversion.
  118.  
  119.                     // Show informations in Riche Text Box ..
  120.                     rtxtBox.Text = "your Totale social media messages is : " + h + "\n\n";
  121.  
  122.                     while (j != h)
  123.                     {
  124.                         // Select All Messages in page
  125.                         driver.FindElement(By.XPath(SelectAll)).Click();
  126.  
  127.                         // Delet Messages Selected
  128.                         driver.FindElement(By.XPath(node_Delet)).Click();
  129.                         Thread.Sleep(12000);
  130.  
  131.                         // Restart Value
  132.                         nbrValue = driver.FindElement(By.XPath(node_NbrOfmessage));
  133.                         nbrOfPage = driver.FindElement(By.XPath(node_NbrOfPages));
  134.                         nbr_Messages = nbrValue.Text;
  135.                         j = Convert.ToInt32(nbr_Messages);
  136.                         nbr_Pages = nbrOfPage.Text;
  137.                         nbr_Pages = Regex.Replace(nbr_Pages, @"\s", "");
  138.                         h = Convert.ToInt32(nbr_Pages);
  139.                     }
  140.  
  141.                     if (j == h)
  142.                     {
  143.                         // Select All Messages in page
  144.                         driver.FindElement(By.XPath(SelectAll)).Click();
  145.  
  146.                         // Delet Messages Selected
  147.                         driver.FindElement(By.XPath(node_Delet)).Click();
  148.                     }
  149.                 }
  150.                 else
  151.                     rtxtBox.Text = "Your \"Social Networks\" tab is empty";
  152.  
  153.                 // Click to Promotions Messages
  154.                 string value_Promotion = "//*[@data-tooltip='Promotions, offres et autres messages commerciaux.']";
  155.                 driver.FindElement(By.XPath(value_Promotion)).Click();
  156.  
  157.                 // see if messages PROMO exit or not
  158.                 slide = driver.FindElement(By.XPath("//*[@class='ar5 J-J5-Ji']"));
  159.                 vide = slide.Text;
  160.                 if (vide != " ")
  161.                 {
  162.                     // Declare number of Messages in page
  163.                     string node_NbrOfmessageP = "//div[@aria-label='Afficher plus de messages']/span/span[1]/span[2]";
  164.                     IWebElement nbrValueP = driver.FindElement(By.XPath(node_NbrOfmessageP));
  165.  
  166.                     // Declare number of pages Messages
  167.                     string node_NbrOfPagesP = "//div[@aria-label='Afficher plus de messages']/span/span[2]";
  168.                     IWebElement nbrOfPageP = driver.FindElement(By.XPath(node_NbrOfPagesP));
  169.  
  170.                     // Start Conversion
  171.                     // convert number of messages in page to integer
  172.                     string nbr_MessagesP = nbrValueP.Text;
  173.                     int jj = Convert.ToInt32(nbr_MessagesP);
  174.  
  175.                     // convert number of pages messages to integer
  176.                     string nbr_PagesP = nbrOfPageP.Text;
  177.                     nbr_PagesP = Regex.Replace(nbr_PagesP, @"\s", "");
  178.                     int hh = Convert.ToInt32(nbr_PagesP);
  179.                     // End Conversion.
  180.  
  181.                     // Show informations in Riche Text Box ..
  182.                     rtxtBox2.Text = "And your Totale Promotions messages is :  " + hh + "\n\n";
  183.  
  184.                     while (jj != hh)
  185.                     {
  186.                         // Select All Messages in page
  187.                         driver.FindElement(By.XPath(SelectAll)).Click();
  188.  
  189.                         // Delet Messages Selected
  190.                         driver.FindElement(By.XPath(node_Delet)).Click();
  191.                         Thread.Sleep(12000);
  192.  
  193.                         // Restart Value
  194.                         nbrValueP = driver.FindElement(By.XPath(node_NbrOfmessageP));
  195.                         nbrOfPageP = driver.FindElement(By.XPath(node_NbrOfPagesP));
  196.                         nbr_MessagesP = nbrValueP.Text;
  197.                         jj = Convert.ToInt32(nbr_MessagesP);
  198.                         nbr_PagesP = nbrOfPageP.Text;
  199.                         nbr_PagesP = Regex.Replace(nbr_PagesP, @"\s", "");
  200.                         hh = Convert.ToInt32(nbr_PagesP);
  201.                     }
  202.  
  203.                     if (jj == hh)
  204.                     {
  205.                         // Select All Messages in page
  206.                         driver.FindElement(By.XPath(SelectAll)).Click();
  207.  
  208.                         // Delet Messages Selected
  209.                         driver.FindElement(By.XPath(node_Delet)).Click();
  210.                         Thread.Sleep(1000);
  211.                     }
  212.                 }
  213.                 else
  214.                     rtxtBox2.Text = "Your \"Promotions\" tab is empty";
  215.  
  216.                 // Close Browser & Driver
  217.                 driver.Close();
  218.                 driver.Quit();
  219.                 // Show Message << your social media messages was deleted >> ..
  220.                 MessageBox.Show("Social Media & Promotions Messages was successful deleted"
  221.                     + "\n\n" + "Gongralutation ..",
  222.                     "Messages Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information);
  223.             }
  224.             catch (Exception ex)
  225.             {
  226.                 MessageBox.Show(ex.Message);
  227.             }
  228.  
  229.             this.Enabled = true;
  230.             this.Cursor = Cursors.Default;
  231.         }
  232.  
  233.         private void btn_Exit_Click(object sender, EventArgs e)
  234.         {
  235.             Application.Exit();
  236.         }
  237.  
  238.         private void txtBox_Pass_KeyPress(object sender, KeyPressEventArgs e)
  239.         {
  240.             if (e.KeyChar == (char)Keys.Enter)
  241.             {
  242.                 btn_Login.PerformClick();
  243.             }
  244.         }
  245.  
  246.         private void btnAbout_Click(object sender, EventArgs e)
  247.         {
  248.             AboutBox frmAbout = new AboutBox();
  249.             frmAbout.ShowDialog();      
  250.         }
  251.  
  252.         private void btnShow2_Click(object sender, EventArgs e)
  253.         {
  254.             txtBox_Pass.PasswordChar = '\0';
  255.             btnShow2.Visible = false;
  256.             labelShow.Visible = false;
  257.         }
  258.  
  259.         private void btnShow_Click(object sender, EventArgs e)
  260.         {
  261.             txtBox_Pass.PasswordChar = char.Parse("◉");
  262.             btnShow2.Visible = true;
  263.             labelShow.Visible = true;
  264.         }
  265.  
  266.         private void txtBox_Pass_KeyUp(object sender, KeyEventArgs e)
  267.         {
  268.             // Enable & Diable Button Show
  269.             //if (txtBox_Pass.TextLength > 1)
  270.             //    btn_Login.Enabled = true;
  271.             //else
  272.             //    btn_Login.Enabled = false;
  273.         }
  274.  
  275.         private void AboutHover1_MouseHover(object sender, EventArgs e)
  276.         {
  277.             AboutHover1.Visible = false;
  278.         }
  279.  
  280.         private void btnAbout_MouseLeave(object sender, EventArgs e)
  281.         {
  282.             AboutHover1.Visible = true;
  283.         }
  284.     }
  285. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement