import time from selenium import webdriver from selenium.webdriver.common.keys import Keys driver = webdriver.Chrome() driver.implicitly_wait(10) driver.get("https://mail.google.com/") elem = driver.find_element_by_id("identifierId") elem.clear() elem.send_keys("YourEmailId@gmail.com") elem = driver.find_element_by_id("identifierNext") elem.click() elem = driver.find_element_by_name("password") #elem.clear() elem.send_keys("YourPassword") elem.send_keys(Keys.RETURN) elem = driver.find_element_by_name("q") #elem.clear() elem.send_keys("label:YourLabel") elem.send_keys(Keys.RETURN) for x in range(1,50): elem = driver.find_element_by_xpath("//div[@id=':rr']/div/span/div") elem.click() time.sleep(5) elem = driver.find_element_by_xpath("//div[@id=':5']/div/div/div/div/div/div/div/div/div") elem.click() time.sleep(5)
Now the story:
I had a large number of mails coming from a specific mailing group. I had created a label and rule label these mails with that label in Gmail. Today, when I opened Gmail, I found that I had used almost 100% of my storage. I first wrote an google app script to fix this issue. However, I soon came across the script invocation limitations set by google. So, I installed selenium for python, got the chromedriver and put it in the path and wrote the above script. Wonder where I got stuff like "//div[@id=':5']/div/div/div/div/div/div/div/div/div"? I was trying to use selenium ide with FireFox. Though the IDE did not work, it gave me such useful info as above.
0. Install python
1. Install selenium bindings for python (pip install selenium)
2. Install selenium driver for your browser (Download and extract to some folder. Then add that folder to your PATH)
3. Write this script (Use inspect element option in chrome, firefox selenium ide etc to know the elements that you want to manipulate)
4. Debug, fix issues and have fun!
Here are some useful links:
Note: You will have to go to trash after a while and click on empty trash.