Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Gmail Automation with Explicit wait
- // Version 1.1
- using System;
- using System.Windows.Forms;
- using OpenQA.Selenium;
- using OpenQA.Selenium.Chrome;
- using OpenQA.Selenium.Support.UI;
- using System.Text.RegularExpressions;
- using Keys = System.Windows.Forms.Keys;
- namespace Gmail_Scrape
- {
- public partial class frm_Gmail : Form
- {
- public frm_Gmail()
- {
- InitializeComponent();
- }
- private void btn_Login_Click(object sender, EventArgs e)
- {
- this.Enabled = false;
- this.Cursor = Cursors.WaitCursor;
- using (IWebDriver driver = new ChromeDriver())
- {
- try
- {
- // Explicit Waits
- WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
- // Navigate to Gmail.com
- driver.Navigate().GoToUrl("https://mail.google.com/mail/");
- // email
- string email = txtBox_User.Text;
- // password
- string pass = txtBox_Pass.Text;
- // Maximize Navigator
- driver.Manage().Window.Maximize();
- // Enter Email and click next button
- IWebElement emailSend = driver.FindElement(By.XPath("//*[@type='email']"));
- emailSend.SendKeys(email);
- IWebElement emailNextClick = driver.FindElement(By.XPath("//*[@id='identifierNext']"));
- emailNextClick.Click();
- // Enter Password and click next button
- IWebElement passSend = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@type='password']")));
- passSend.SendKeys(pass);
- IWebElement passNextClick = driver.FindElement(By.XPath("//*[@id='passwordNext']"));
- passNextClick.Click();
- // Click to Social Media Messages
- 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.";
- IWebElement value_SMClick = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@data-tooltip='" + value_SM + "']")));
- value_SMClick.Click();
- // Node of button delete
- string node_Delet = "//div[@class='nH aqK']/div/div/div/div[2]/div[3]";
- // Node of Selectionner Tout
- string SelectAll = "//*[@aria-label='Sélectionner']/div[1]/span";
- // see if messages SM exit or not
- IWebElement slide = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@class='ar5 J-J5-Ji']")));
- string vide = slide.Text;
- if (vide != " ")
- {
- // Declare number of Messages in page
- string node_NbrOfmessage = "//div[@aria-label='Afficher plus de messages']/span/span[1]/span[2]";
- IWebElement nbrValue = driver.FindElement(By.XPath(node_NbrOfmessage));
- // Declare number of pages Messages
- string node_NbrOfPages = "//div[@aria-label='Afficher plus de messages']/span/span[2]";
- IWebElement nbrOfPage = driver.FindElement(By.XPath(node_NbrOfPages));
- // Start Conversion
- // convert number of messages in page to integer
- string nbr_Messages = nbrValue.Text;
- int j = Convert.ToInt32(nbr_Messages);
- // convert number of pages messages to integer
- string nbr_Pages = nbrOfPage.Text;
- // Replace space
- nbr_Pages = Regex.Replace(nbr_Pages, @"\s", "");
- int h = Convert.ToInt32(nbr_Pages);
- // End Conversion.
- // Show informations in Riche Text Box ..
- rtxtBox.Text = "your Totale social media messages is : " + h + "\n\n";
- while (j != h)
- {
- // Select All Messages in page
- IWebElement selectAllClick = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(SelectAll)));
- selectAllClick.Click();
- // Delet Messages Selected
- IWebElement DeletClick = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(node_Delet)));
- DeletClick.Click();
- // Restart Value
- nbrValue = driver.FindElement(By.XPath(node_NbrOfmessage));
- nbrOfPage = driver.FindElement(By.XPath(node_NbrOfPages));
- nbr_Messages = nbrValue.Text;
- j = Convert.ToInt32(nbr_Messages);
- nbr_Pages = nbrOfPage.Text;
- nbr_Pages = Regex.Replace(nbr_Pages, @"\s", "");
- h = Convert.ToInt32(nbr_Pages);
- }
- if (j == h)
- {
- // Select All Messages in page
- IWebElement selectAllClick = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(SelectAll)));
- selectAllClick.Click();
- // Delet Messages Selected
- IWebElement DeletClick = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(node_Delet)));
- DeletClick.Click();
- }
- }
- else
- rtxtBox.Text = "Your \"Social Networks\" tab is empty";
- // Click to Promotions Messages
- string value_Promotion = "//*[@data-tooltip='Promotions, offres et autres messages commerciaux.']";
- IWebElement PromotionClick = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(value_Promotion)));
- PromotionClick.Click();
- // see if messages PROMO exit or not
- slide = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@class='ar5 J-J5-Ji']")));
- vide = slide.Text;
- if (vide != " ")
- {
- // Declare number of Messages in page
- string node_NbrOfmessageP = "//div[@aria-label='Afficher plus de messages']/span/span[1]/span[2]";
- IWebElement nbrValueP = driver.FindElement(By.XPath(node_NbrOfmessageP));
- // Declare number of pages Messages
- string node_NbrOfPagesP = "//div[@aria-label='Afficher plus de messages']/span/span[2]";
- IWebElement nbrOfPageP = driver.FindElement(By.XPath(node_NbrOfPagesP));
- // Start Conversion
- // convert number of messages in page to integer
- string nbr_MessagesP = nbrValueP.Text;
- int jj = Convert.ToInt32(nbr_MessagesP);
- // convert number of pages messages to integer
- string nbr_PagesP = nbrOfPageP.Text;
- nbr_PagesP = Regex.Replace(nbr_PagesP, @"\s", "");
- int hh = Convert.ToInt32(nbr_PagesP);
- // End Conversion.
- // Show informations in Riche Text Box ..
- rtxtBox2.Text = "And your Totale Promotions messages is : " + hh + "\n\n";
- while (jj != hh)
- {
- // Select All Messages in page
- IWebElement SelectAllClick = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(SelectAll)));
- SelectAllClick.Click();
- // Delet Messages Selected
- IWebElement DeletClick = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(node_Delet)));
- DeletClick.Click();
- // Restart Value
- nbrValueP = driver.FindElement(By.XPath(node_NbrOfmessageP));
- nbrOfPageP = driver.FindElement(By.XPath(node_NbrOfPagesP));
- nbr_MessagesP = nbrValueP.Text;
- jj = Convert.ToInt32(nbr_MessagesP);
- nbr_PagesP = nbrOfPageP.Text;
- nbr_PagesP = Regex.Replace(nbr_PagesP, @"\s", "");
- hh = Convert.ToInt32(nbr_PagesP);
- }
- if (jj == hh)
- {
- // Select All Messages in page
- IWebElement SelectAllClick = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(SelectAll)));
- SelectAllClick.Click();
- // Delet Messages Selected
- IWebElement DeletClick = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(node_Delet)));
- DeletClick.Click();
- }
- }
- else
- rtxtBox2.Text = "Your \"Promotions\" tab is empty";
- // Close Browser & Driver
- driver.Close();
- driver.Quit();
- // Show Message << your social media messages was deleted >> ..
- MessageBox.Show("Social Media & Promotions Messages was successful deleted"
- + "\n\n" + "Gongralutation ..",
- "Messages Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- this.Enabled = true;
- this.Cursor = Cursors.Default;
- }
- private void btn_Exit_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
- private void txtBox_Pass_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (e.KeyChar == (char)Keys.Enter)
- {
- btn_Login.PerformClick();
- }
- }
- private void btnAbout_Click(object sender, EventArgs e)
- {
- AboutBox frmAbout = new AboutBox();
- frmAbout.ShowDialog();
- }
- private void btnShow2_Click(object sender, EventArgs e)
- {
- txtBox_Pass.PasswordChar = '\0';
- btnShow2.Visible = false;
- labelShow.Visible = false;
- }
- private void btnShow_Click(object sender, EventArgs e)
- {
- txtBox_Pass.PasswordChar = char.Parse("◉");
- btnShow2.Visible = true;
- labelShow.Visible = true;
- }
- private void AboutHover1_MouseHover(object sender, EventArgs e)
- {
- AboutHover1.Visible = false;
- }
- private void btnAbout_MouseLeave(object sender, EventArgs e)
- {
- AboutHover1.Visible = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement