Selenium案例:自动批量删除 Github Repository

2019-08-22

近期整理 Github 时发现,不知何时起,在上面扔了一大堆项目,遂想删除之,但是删除一个 Github Repo 步骤太繁琐了,不如写个自动化脚本去执行吧(虽然最终写代码的耗时比手动删除还慢,但是好歹又练习了一下 Python 不是?)

ChromeDriver 安装

下载地址:https://npm.taobao.org/mirrors/chromedriver/ 选择最新的版本,与本地 Chrome 浏览器大版本号一致基本没有什么问题。

# 移动可执行文件到 PATH 路径中,算是完成了安装
mv chromedriver /usr/local/bin

Python Selenium 包安装

  ~ pip3 install selenium
Collecting selenium
  Downloading https://files.pythonhosted.org/packages/80/d6/4294f0b4bce4de0abf13e17190289f9d0613b0a44e5dd6a7f5ca98459853/selenium-3.141.0-py2.py3-none-any.whl (904kB)
    100% |████████████████████████████████| 911kB 9.5MB/s 
Collecting urllib3 (from selenium)
  Using cached https://files.pythonhosted.org/packages/e6/60/247f23a7121ae632d62811ba7f273d0e58972d75e58a94d329d51550a47d/urllib3-1.25.3-py2.py3-none-any.whl
Installing collected packages: urllib3, selenium
Successfully installed selenium-3.141.0 urllib3-1.25.3

编写 Selenium 脚本

  • auto-remove-github-repo.py
#!/usr/bin/python3
# -*- coding: utf-8 -*
from selenium import webdriver
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.common.action_chains import ActionChains


def start():
    chrome_webdriver = get_webdriver()
    login(chrome_webdriver)

    projects = [
        "wenzewoo/gowebwx",
        "wenzewoo/goexec",
        "wenzewoo/intellij-flat-themes",
        "wenzewoo/blogstroe",
        "wenzewoo/jmeter",
        "wenzewoo/cleansheet4idea",
        "wenzewoo/yuque-hexo",
        "wenzewoo/user-agent-utils",
        "wenzewoo/gobuffalo-docs",
        "wenzewoo/goddos",
        "wenzewoo/boilerplate",
        "wenzewoo/ngrok",
        "wenzewoo/jmeter-plugins",
        "wenzewoo/incubator-dubbo",
        "wenzewoo/markdown-support-qiniu",
        "wenzewoo/proxylist",
        "wenzewoo/Vue-FM",
        "wenzewoo/Eclipse-SimpleTranslation",
        "wenzewoo/MQTT_EXAMPLES",
        "wenzewoo/TranslationPlugin",
        "wenzewoo/Base64Image",
        "wenzewoo/retrolambda",
        "wenzewoo/wechat-tmplmsg",
        "wenzewoo/WifiViewer",
        "wenzewoo/easyui-layout-demo",
        "wenzewoo/RemoteDebug",
        "wenzewoo/slowhttptest-for-docker",
        "wenzewoo/excel-export",
        "wenzewoo/IntelliJ-IDEA-Tutorial",
        "wenzewoo/android-utils",
        "wenzewoo/LollipopShowcase",
        "wenzewoo/Android-Validator"
    ]
    do_remove_projects(projects, chrome_webdriver)
    chrome_webdriver.quit()


def get_webdriver():
    chrome_webdriver = webdriver.Chrome()
    chrome_webdriver.implicitly_wait(10)
    return chrome_webdriver


def login(chrome: WebDriver):
    chrome.get("https://github.com/login")
    chrome.find_element_by_id("login_field").send_keys("wenzewoo")
    chrome.find_element_by_id("password").send_keys("密码密码")
    chrome.find_element_by_css_selector("input[type=submit]").click()


def do_remove_projects(projects: list, chrome: WebDriver):
    for i, project in enumerate(projects):
        chrome.get("https://github.com/%s/settings" % project)
        chrome.execute_script("document.documentElement.scrollTop=100000")

        is_click = False
        for _, option_btn in enumerate(chrome.find_elements_by_class_name("btn-danger")):
            if option_btn.text == 'Delete this repository':
                option_btn.click()
                is_click = True
                break

        if not is_click:
            print("Cannot find delete button, project = %s" % project)
            continue

        # 此文本框包含 hidden 属性,不能直接 send_keys, 先将鼠标移动到该位置,然后输入.
        # ElementNotVisibleException: Message: element not visible
        verify_text = chrome.find_element_by_css_selector('input[name=verify]')
        ActionChains(chrome).move_to_element(verify_text).send_keys(project).perform()

        for _, option_btn in enumerate(chrome.find_elements_by_class_name("btn-danger")):
            if option_btn.text == 'I understand the consequences, delete this repository':
                option_btn.click()


if __name__ == '__main__':
    start()

执行完毕后,世界清净了。。