通过selenium自动发送博客

通过selenium自动发博客

from selenium import webdriver
import time
bro=webdriver.Chrome(executable_path='./chromedriver.exe')  
bro.get('https://i.cnblogs.com/')   #打开管理页
bro.implicitly_wait(10)  #设置隐形等待

这是首页,我们通过qq登录只需要找到qq的所在的标签,点击即可,

qq_login = bro.find_element_by_xpath("/html/body/app-root/mat-sidenav-container/mat-sidenav-content/div/div/app-sign-in/app-content-container/mat-card/div/div[2]/div[2]/div/button[2]")  #直接复制xpath 简单暴力
qq_login.click()  # 点击

因为打开了点击了这个需要打开新的窗口,我们需要切换到这个窗口

n = bro.window_handles # 获取当前页句柄
print (n)
bro.switch_to.window (n[1])  #   切换到新的窗口  

现在只需要点击我的头像,就可以登陆了,但是由于frame相当于一个单独的网页,在父frame里是无法直接查看到子frame的元素的,必须switch_to_frame切到该frame下,才能进一步查找,

 

左边的部分是在这里:切换到这个frame

bro.switch_to.frame('ptlogin_iframe')  #切换到这个frame
qq_t = bro.find_element_by_id("img_out_424352752")  #找到这个图片,
time.sleep(1)  #睡一秒休息一会  
qq_t.click()  #点击登录

点击登陆后,他会自动登录到首页:

bro.switch_to.window (n[0])   #切换
add_tag = bro.find_element_by_xpath("/html/body/cnb-root/cnb-layout/div[2]/div[3]/div[1]/cnb-sidebar[1]/div/ul/li[1]/a")  #这里是找到添加新随笔,方法多种css,id,class,xpath等,
add_tag.click()  点击

正文内容:找到正文的标签,还有分类的标签,填写内容,或者,发布

找到发布:

title_tag = bro.find_element_by_id("post-title")  #右键检查,找到标题
title_tag.send_keys("selenium自动发布博客的测试")  
bro.switch_to.frame('Editor_Edit_EditorBody_ifr')   #和之前的的一样,需要切换到frame,
body_tag = bro.find_element_by_xpath("//*[@id='tinymce']")  #找到文章主体,
body_tag.send_keys("测试测试")  #写上测试内容,
bro.switch_to.window (n[0])    #切换回页句柄
tag_f = bro.find_element_by_xpath("/html/body/cnb-root/cnb-layout/div[2]/div[3]/div[2]/div/cnb-spinner/div/cnb-post-editing-v2/cnb-post-editor/div[2]/cnb-collapse-panel[1]/div[2]/cnb-category-selector-panel/cnb-collapse-panel/div[2]/cnb-category-selector/div/div[1]/label/input")  #找到点击分类
tag_f.click()  #点击
#"Editor_Edit_EditorBody_ifr"

post_tag = bro.find_element_by_xpath("/html/body/cnb-root/cnb-layout/div[2]/div[3]/div[2]/div/cnb-spinner/div/cnb-post-editing-v2/cnb-post-editor/div[3]/cnb-spinner/div/cnb-submit-buttons/button[1]")  #找到发送的标签
post_tag.click() 点击发送

可以结合爬虫,爬取别人的博客,发到自己的博客,学习的事,怎么能叫抄呢,分享一个爬虫爬到的小姐姐:

原文地址:https://www.cnblogs.com/liqiangwei/p/14335026.html