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[2]/div/div/div/div/div[2]/div[3]/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[2]/div/div/div/div/div[2]/div[3]/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.
Steps:
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:
1. http://selenium-python.readthedocs.io/installation.html
2. https://sites.google.com/a/chromium.org/chromedriver/downloads
3. http://selenium-python.readthedocs.io/getting-started.html
4. http://www.seleniumhq.org/projects/ide/
5. http://selenium-python.readthedocs.io/navigating.html
6. https://stackoverflow.com/questions/17533024/how-to-set-selenium-python-webdriver-default-timeout
7. https://www.tutorialspoint.com/python/time_sleep.htm
Note: You will have to go to trash after a while and click on empty trash.