Advertisement
kilya

gmailAutomation_New_Version

Feb 12th, 2019
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.27 KB | None | 0 0
  1. using System;
  2. using System.Windows.Forms;
  3. using OpenQA.Selenium;
  4. using OpenQA.Selenium.Chrome;
  5. using OpenQA.Selenium.Support.UI;
  6. using System.Text.RegularExpressions;
  7. using Keys = System.Windows.Forms.Keys;
  8.  
  9. namespace Gmail_Scrape
  10. {
  11.     public partial class frm_Gmail : Form
  12.     {
  13.         IWebDriver driver;
  14.         public frm_Gmail()
  15.         {
  16.             InitializeComponent();
  17.             // version 1.2
  18.         }
  19.  
  20.         private void btn_Login_Click(object sender, EventArgs e)
  21.         {
  22.             this.Enabled = false;
  23.             this.Cursor = Cursors.WaitCursor;
  24.             using (driver = new ChromeDriver())
  25.             {
  26.                 try
  27.                 {
  28.                     // Explicit Waits
  29.                     WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(120));
  30.                     // Navigate to Gmail.com
  31.                     driver.Navigate().GoToUrl("https://mail.google.com/mail/");
  32.                    
  33.                     // email
  34.                     string email = txtBox_User.Text;
  35.                     // password
  36.                     string pass = txtBox_Pass.Text;
  37.  
  38.                     // Maximize Navigator
  39.                     driver.Manage().Window.Maximize();
  40.  
  41.                     // Enter Email and click next button
  42.                     IWebElement emailSend = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@type='email']")));
  43.                     emailSend.SendKeys(email);
  44.                     IWebElement emailNextClick = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@id='identifierNext']")));
  45.                     emailNextClick.Click();
  46.  
  47.                     // Enter Password and click next button
  48.                     IWebElement passSend = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@type='password']")));
  49.                     passSend.SendKeys(pass);
  50.                     IWebElement passNextClick = driver.FindElement(By.XPath("//*[@id='passwordNext']"));
  51.                     passNextClick.Click();
  52.  
  53.                     // Click to Social Media Messages
  54.                     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.";
  55.                     IWebElement value_SMClick = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@data-tooltip='" + value_SM + "']")));
  56.                     value_SMClick.Click();
  57.  
  58.                     // Node of button delete
  59.                     string node_Delet = "//div[@class='nH aqK']/div/div/div/div[2]/div[3]";
  60.                     // Node of Selectionner Tout
  61.                     string SelectAll = "//*[@aria-label='Sélectionner']/div[1]/span";
  62.  
  63.                     // see if messages SM exit or not
  64.                     IWebElement slide = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@class='ar5 J-J5-Ji']")));
  65.                     string vide = slide.Text;
  66.                     string rtextSM = "Your \"Social Networks\" tab is empty";
  67.                     string totaleSM = "your Totale social media messages is : ";
  68.                     SelectAndDelet_All(driver, vide, SelectAll, node_Delet, totaleSM, rtextSM);
  69.                    
  70.                     // Click to Promotions Messages
  71.                     string value_Promotion = "//*[@data-tooltip='Promotions, offres et autres messages commerciaux.']";
  72.                     IWebElement PromotionClick = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(value_Promotion)));
  73.                     PromotionClick.Click();
  74.                     // see if messages PROMO exit or not
  75.                     slide = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@class='ar5 J-J5-Ji']")));
  76.                     vide = slide.Text;
  77.                     string rtextPr = "Your \"Promotions\" tab is empty";
  78.                     string totalePr = "And your Totale Promotions messages is : ";
  79.                     SelectAndDelet_All(driver, vide, SelectAll, node_Delet,totalePr, rtextPr);
  80.                    
  81.                     // Close Browser & Driver
  82.                     driver.Close();
  83.                     driver.Quit();
  84.                     // Show Message << your social media messages was deleted >> ..
  85.                     MessageBox.Show("Social Media & Promotions Messages was successful deleted"
  86.                         + "\n\n" + "Gongralutation ..",
  87.                         "Messages Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information);
  88.                 }
  89.                 catch (Exception ex)
  90.                 {
  91.                     MessageBox.Show(ex.Message);
  92.                 }
  93.             }    
  94.             this.Enabled = true;
  95.             this.Cursor = Cursors.Default;
  96.         }
  97.  
  98.         private void SelectAndDelet_All(IWebDriver driver, string vide, string SelectAll, string node_Delet, string totale, string rtext)
  99.         {
  100.             WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(120));
  101.             bool b = Regex.IsMatch(totale, "social");
  102.  
  103.             if (vide != " ")
  104.             {
  105.                 // Declare number of Messages in page
  106.                 string node_NbrOfmessage = "//div[@aria-label='Afficher plus de messages']/span/span[1]/span[2]";
  107.                 IWebElement nbrValue = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(node_NbrOfmessage)));
  108.  
  109.                 // Declare number of pages Messages
  110.                 string node_NbrOfPages = "//div[@aria-label='Afficher plus de messages']/span/span[2]";
  111.                 IWebElement nbrOfPage = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(node_NbrOfPages)));
  112.  
  113.                 // Start Conversion
  114.                 // convert number of messages in page to integer
  115.                 string nbr_Messages = nbrValue.Text;
  116.                 int j = Convert.ToInt32(nbr_Messages);
  117.  
  118.                 // convert number of pages messages to integer
  119.                 string nbr_Pages = nbrOfPage.Text;
  120.                 // Replace space
  121.                 nbr_Pages = Regex.Replace(nbr_Pages, @"\s", "");
  122.                 int h = Convert.ToInt32(nbr_Pages);
  123.                 // End Conversion.
  124.  
  125.                 // Show informations in Riche Text Box ..
  126.                 if (b)
  127.                     rtxtBox.Text = totale + h;
  128.                 else
  129.                     rtxtBox2.Text = totale + h;
  130.  
  131.                 while (j != h)
  132.                 {
  133.                     // Select All Messages in page
  134.                     IWebElement selectAllClick = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(SelectAll)));
  135.                     selectAllClick.Click();
  136.                     // Delet Messages Selected
  137.                     IWebElement DeletClick = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(node_Delet)));
  138.                     DeletClick.Click();
  139.  
  140.                     // Restart Value
  141.                     nbrValue = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(node_NbrOfmessage)));
  142.                     nbrOfPage = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(node_NbrOfPages)));
  143.                     nbr_Messages = nbrValue.Text;
  144.                     j = Convert.ToInt32(nbr_Messages);
  145.                     nbr_Pages = nbrOfPage.Text;
  146.                     nbr_Pages = Regex.Replace(nbr_Pages, @"\s", "");
  147.                     h = Convert.ToInt32(nbr_Pages);
  148.                 }
  149.  
  150.                 if (j == h)
  151.                 {
  152.                     // Select All Messages in page
  153.                     IWebElement selectAllClick = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(SelectAll)));
  154.                     selectAllClick.Click();
  155.                     // Delet Messages Selected
  156.                     IWebElement DeletClick = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(node_Delet)));
  157.                     DeletClick.Click();
  158.                 }
  159.             }
  160.             else
  161.             {
  162.                 if (b)
  163.                     rtxtBox.Text = rtext;
  164.                 else
  165.                     rtxtBox2.Text = rtext;
  166.             }
  167.         }
  168.  
  169.         private void btn_Exit_Click(object sender, EventArgs e)
  170.         {
  171.             Application.Exit();
  172.         }
  173.  
  174.         private void txtBox_Pass_KeyPress(object sender, KeyPressEventArgs e)
  175.         {
  176.             if (e.KeyChar == (char)Keys.Enter)
  177.             {
  178.                 btn_Login.PerformClick();
  179.             }
  180.         }
  181.  
  182.         private void btnAbout_Click(object sender, EventArgs e)
  183.         {
  184.             AboutBox frmAbout = new AboutBox();
  185.             frmAbout.ShowDialog();      
  186.         }
  187.  
  188.         private void btnShow2_Click(object sender, EventArgs e)
  189.         {
  190.             txtBox_Pass.PasswordChar = '\0';
  191.             btnShow2.Visible = false;
  192.             labelShow.Visible = false;
  193.         }
  194.  
  195.         private void btnShow_Click(object sender, EventArgs e)
  196.         {
  197.             txtBox_Pass.PasswordChar = char.Parse("◉");
  198.             btnShow2.Visible = true;
  199.             labelShow.Visible = true;
  200.         }
  201.  
  202.         private void AboutHover1_MouseHover(object sender, EventArgs e)
  203.         {
  204.             AboutHover1.Visible = false;
  205.         }
  206.  
  207.         private void btnAbout_MouseLeave(object sender, EventArgs e)
  208.         {
  209.             AboutHover1.Visible = true;
  210.         }
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement