· 7 years ago · Mar 09, 2019, 04:52 PM
1using System;
2using System.Windows.Forms;
3using OpenQA.Selenium;
4using OpenQA.Selenium.Chrome;
5using OpenQA.Selenium.Support.UI;
6using System.Text.RegularExpressions;
7using Keys = System.Windows.Forms.Keys;
8
9namespace Gmail_Scrape
10{
11 public partial class frm_Gmail : Form
12 {
13 IWebDriver driver;
14
15 public frm_Gmail()
16 {
17 InitializeComponent();
18 }
19
20 private void btn_Login_Click(object sender, EventArgs e)
21 {
22 // Enable Form and Change cursor
23 this.Enabled = false;
24 this.Cursor = Cursors.WaitCursor;
25 using (driver = new ChromeDriver())
26 {
27 try
28 {
29 // Satrt Explicit Waits
30 WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(120));
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 = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@type='email']")));
44 emailSend.SendKeys(email);
45 IWebElement emailNextClick = wait.Until(ExpectedConditions.ElementToBeClickable(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 IWebElement value_SMClick = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//tr[@role='tablist']/td[2]")));
56 value_SMClick.Click();
57
58 // Node of Selectionner Tout
59 string SelectAll = "//div[@role='button' and @style='user-select: none;']/div[1]/span";
60 // Node of button delete
61 //string node_Delet = "//div[@class='nH aqK']/div/div/div/div[2]/div[3]";
62 string node_Delet = "//div[@act='10' and @role='button']/div";
63
64
65 // see if messages SM exit or not
66 IWebElement slide = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@class='ar5 J-J5-Ji']")));
67 string vide = slide.Text;
68 string rtextSM = "Your \"Social Networks\" tab is empty";
69 string totaleSM = "your Totale social media messages is : ";
70 SelectAndDelet_All(driver, vide, SelectAll, node_Delet, totaleSM, rtextSM);
71
72 // Click to Promotions Messages
73 IWebElement PromotionClick = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//tr[@role='tablist']/td[3]")));
74 PromotionClick.Click();
75 // see if messages PROMO exit or not
76 slide = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//*[@class='ar5 J-J5-Ji']")));
77 vide = slide.Text;
78 string rtextPr = "Your \"Promotions\" tab is empty";
79 string totalePr = "And your Totale Promotions messages is : ";
80 SelectAndDelet_All(driver, vide, SelectAll, node_Delet,totalePr, rtextPr);
81
82 // Close Browser & Driver
83 driver.Close();
84 driver.Quit();
85 // Show Message << your social media messages was deleted >> ..
86 MessageBox.Show("Social Media & Promotions Messages was successful deleted"
87 + "\n\n" + "Gongralutation ..",
88 "Messages Deleted", MessageBoxButtons.OK, MessageBoxIcon.Information);
89 }
90 catch (Exception ex)
91 {
92 MessageBox.Show(ex.Message);
93 }
94 }
95
96 // Default value
97 this.Enabled = true;
98 this.Cursor = Cursors.Default;
99 }
100
101 private void SelectAndDelet_All(IWebDriver driver, string vide, string SelectAll, string node_Delet, string totale, string rtext)
102 {
103 WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(120));
104 bool b = Regex.IsMatch(totale, "social");
105
106 if (vide != " ")
107 {
108 // Declare number of Messages in page
109 // Old Xpath
110 //string node_NbrOfmessage = "//div[@aria-label='Afficher plus de messages']/span/span[1]/span[2]";
111 // New Xpath : Number of messages in page
112 string node_NbrOfmessage = "//*[@style='position: relative;']/div[2]/div[1]/div[2]/div[1]/div[1]/div[1]/" +
113 "div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/span[1]/div[1]/span[1]/span[1]/span[2]";
114 IWebElement nbrValue = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(node_NbrOfmessage)));
115
116 // Declare number of pages messages
117 // Old Xpath
118 //string node_NbrOfPages = "//div[@aria-label='Afficher plus de messages']/span/span[2]";
119 // New Xpath : Number of pages messages
120 string node_NbrOfPages = "//*[@style='position: relative;']/div[2]/div[1]/div[2]/div[1]/div[1]/div[1]/" +
121 "div[1]/div[1]/div[1]/div[1]/div[2]/div[1]/span[1]/div[1]/span[1]/span[2]";
122 IWebElement nbrOfPage = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(node_NbrOfPages)));
123
124 // Start Conversion
125 // convert number of messages in page to integer
126 string nbr_Messages = nbrValue.Text;
127 int j = Convert.ToInt32(nbr_Messages);
128
129 // convert number of pages messages to integer
130 string nbr_Pages = nbrOfPage.Text;
131 // Replace space
132 nbr_Pages = Regex.Replace(nbr_Pages, @"\s", "");
133 int h = Convert.ToInt32(nbr_Pages);
134 // End Conversion.
135
136 // Show informations in Riche Text Box ..
137 if (b)
138 rtxtBox.Text = totale + h;
139 else
140 rtxtBox2.Text = totale + h;
141
142 while (j != h)
143 {
144 // Select All Messages in page
145 IWebElement selectAllClick = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(SelectAll)));
146 selectAllClick.Click();
147 // Delet Messages Selected
148 IWebElement DeletClick = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(node_Delet)));
149 DeletClick.Click();
150
151 // Restart Value
152 nbrValue = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(node_NbrOfmessage)));
153 nbrOfPage = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(node_NbrOfPages)));
154 nbr_Messages = nbrValue.Text;
155 j = Convert.ToInt32(nbr_Messages);
156 nbr_Pages = nbrOfPage.Text;
157 nbr_Pages = Regex.Replace(nbr_Pages, @"\s", "");
158 h = Convert.ToInt32(nbr_Pages);
159 }
160
161 if (j == h)
162 {
163 // Select All Messages in page
164 IWebElement selectAllClick = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(SelectAll)));
165 selectAllClick.Click();
166 // Delet Messages Selected
167 IWebElement DeletClick = wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(node_Delet)));
168 DeletClick.Click();
169 }
170 }
171 else
172 {
173 // Show informations in Riche Text Box ..
174 if (b)
175 rtxtBox.Text = rtext;
176 else
177 rtxtBox2.Text = rtext;
178 }
179 }
180
181 private void btn_Exit_Click(object sender, EventArgs e)
182 {
183 Application.Exit();
184 }
185
186 private void txtBox_Pass_KeyPress(object sender, KeyPressEventArgs e)
187 {
188 if (e.KeyChar == (char)Keys.Enter)
189 {
190 btn_Login.PerformClick();
191 }
192 }
193
194 private void btnAbout_Click(object sender, EventArgs e)
195 {
196 AboutBox frmAbout = new AboutBox();
197 frmAbout.ShowDialog();
198 }
199
200 private void btnShow2_Click(object sender, EventArgs e)
201 {
202 txtBox_Pass.PasswordChar = '\0';
203 btnShow2.Visible = false;
204 labelShow.Visible = false;
205 }
206
207 private void btnShow_Click(object sender, EventArgs e)
208 {
209 txtBox_Pass.PasswordChar = char.Parse("â—‰");
210 btnShow2.Visible = true;
211 labelShow.Visible = true;
212 }
213
214 private void AboutHover1_MouseHover(object sender, EventArgs e)
215 {
216 AboutHover1.Visible = false;
217 }
218
219 private void btnAbout_MouseLeave(object sender, EventArgs e)
220 {
221 AboutHover1.Visible = true;
222 }
223 }
224}