selenium实现自动发表halo博客文章

前言

由于最近在写关于selenium自动化测试的作业,便萌生了一个想法,使用Python中的selenium库自动操作浏览器发表博客文章,效果还不错,本文章就是使用Python脚本自动发布的。

实现的功能

读取本地markdown文件的内容,一键自动发布到halo博客上面。

待完善的功能

暂不能在发布时选择分类、标签、封面图,只能在博客后台手动操作。

环境准备

  1. halo博客。
  2. Python环境和selenium库(使用pip安装即可)。
  3. Chrome或Firefox浏览器,以及对应的驱动程序,以下代码以Chrome为例。

Python代码

import time
from selenium import webdriver

driverPath = "你的浏览器驱动器路径"
url = "你的博客管理台链接地址"
username = "你的用户名"
password = "你的密码"
filepath = "本地markdown文章路径"
articleTitle = "文章标题"
alias = "文章别名"
abstract = "文章摘要"

fileObject = open(filepath)
try:
    fileContent = fileObject.read()
finally:
    fileObject.close()

driver = webdriver.Chrome(driverPath)
driver.get(url)

# 填写用户名和密码
text = driver.find_elements_by_class_name('ant-input')

text[0].send_keys(username)
text[1].send_keys(password)

# 登录
time.sleep(3)
driver.find_element_by_tag_name('button').click()

# 点击添加文章
time.sleep(3)
driver.find_element_by_css_selector("#app > section > section > main > div > div.content > div > div:nth-"
                                    "child(1) > div:nth-child(1) > div > div > div > div.meta > span.analysis-card"
                                    "-action > a").click()

# 填写文章名
time.sleep(3)
driver.find_element_by_css_selector("#app > section > section > main > div > div.content > div > div > div.ant-row >"
                                    " div > div:nth-child(1) > input").send_keys(articleTitle)

# 填写文章内容
time.sleep(3)
driver.find_element_by_css_selector("#editor > div > div > div.v-note-panel > div.v-note-edit.divarea-wrapper.scroll-"
                                    "style.transition > div > div > textarea").send_keys(fileContent)

# 点击发布按钮
time.sleep(3)
driver.find_element_by_css_selector("#app > section > section > main > div > div.content > div > div > div.ant-pro-"
                                    "footer-toolbar > div:nth-child(2) > button.ant-btn.ant-btn-primary").click()


# 填写文章别名
time.sleep(3)
driver.find_element_by_css_selector("body > div:nth-child(7) > div > div.ant-drawer-content-wrapper > div > div > div"
                                    ".ant-drawer-body > div.post-setting-drawer-content > div:nth-child(1) > div > "
                                    "form > div.ant-row.ant-form-item.ant-form-item-with-help > div.ant-col.ant-form-"
                                    "item-control-wrapper > div > span > input").send_keys(alias)

# 填写文章摘要
time.sleep(3)
driver.find_element_by_css_selector("body > div:nth-child(7) > div > div.ant-drawer-content-wrapper > div > div > div"
                                    ".ant-drawer-body > div.post-setting-drawer-content > div:nth-child(7) > div > form"
                                    " > div > div > div > span > textarea").send_keys(abstract)

# 提交文章
time.sleep(3)
driver.find_element_by_css_selector("body > div:nth-child(7) > div > div.ant-drawer-content-wrapper > div > div > div."
                                    "ant-drawer-body > div.bottom-control > button.ant-btn.ant-btn-primary").click()

使用方法

装好所有环境,把代码中的参数改成你自己的,然后运行即可。

自动发文动态效果图

自动化测试

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×