Advertisement
kilya

Gmail_Automation_Source_code_Correction_v1.1

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