目的:登录cnblog后新增文章后再次删除该文章并验证

代码如下:

 #coding: utf-8
from selenium import webdriver
from time import sleep
import unittest
import time class DeletePost(unittest.TestCase): def setUp(self):
self.dr = webdriver.Chrome()
self.dr.maximize_window() #定义登录方法
def login(self, username, password):
self.dr.get('https://passport.cnblogs.com/user/signin') #cnblog登录页面
self.dr.find_element_by_id('input1').send_keys(username)
self.dr.find_element_by_id('input2').send_keys(password)
self.dr.find_element_by_id('signin').click() #定义新增文章方法
def create_post(self, title, content):
self.dr.get('https://i.cnblogs.com/EditPosts.aspx?opt=1') #cnblog新增文章页面
self.dr.find_element_by_id('Editor_Edit_txbTitle').send_keys(title)
self.set_content(content)
self.dr.find_element_by_id('Editor_Edit_lkbPost').click() #定义输入富文本content方法
def set_content(self, content):
js = 'document.getElementById("Editor_Edit_EditorBody_ifr").contentWindow.document.body.innerHTML=\'%s\'' % (content)
self.dr.execute_script(js) #定义获取文章post-id方法
def create_post_and_return_its_id(self, title, content):
self.create_post(title, content)
tokens = self.dr.find_element_by_css_selector('#TipsPanel_LinkEdit').get_attribute('href').split('=')
return tokens[-1] #所有文章都对应唯一的post-id #验证删除新增的文章
def test_delete_post_success(self):
'''验证删除新增加的Post'''
self.login('kemi_xxxx', 'kemi_xxxx') #cnblog帐号密码
title = 'title %s' %(time.time()) #标题为title和当前时间
content = 'content %s' %(time.time()) #内容为content和当前时间
sleep(5)
post_id = self.create_post_and_return_its_id(title, content) #调用post-id方法并获得相应id
self.dr.get('https://i.cnblogs.com/') #cnblog后台管理页面
row_id = 'post-row-' + post_id #定义文章列表的行id
post = self.dr.find_element_by_id(row_id) #定位到相应行id上
post.find_element_by_xpath("//a[@href='javascript:void(0)']").click() #定位删除并点击
self.dr.switch_to_alert().accept() #点击弹窗中的确定
sleep(2)
post.find_element_by_xpath("//span[@style='color:red']") #定位相应post的删除成功!提示 def tearDown(self):
print('测试完毕!')
self.dr.quit() if __name__ == '__main__':
unittest.main()

效果如下:

用python+selenium登录cnblog后新增文章后再次删除该文章的更多相关文章

  1. 一次完整的自动化登录测试-基于python+selenium进行cnblog的自动化登录测试

    Web登录测试是很常见的测试!手动测试大家再熟悉不过了,那如何进行自动化登录测试呢!本文作者就用python+selenium结合unittest单元测试框架来进行一次简单但比较完整的cnblog自动 ...

  2. 一次简单完整的自动化登录测试-基于python+selenium进行cnblog的自动化登录测试

    Web登录测试是很常见的测试,手动测试大家再熟悉不过了,那如何进行自动化登录测试呢!本文就基于python+selenium结合unittest单元测试框架来进行一次简单但比较完整的cnblog自动化 ...

  3. Python+selenium登录测试

    我们以登录新浪微博为案例来讲解,首先进入登录页面,输入用户名和密码,点击登录按钮,并且获得用户信息以验证是否登录成功. Web地址:https://login.sina.com.cn/signup/s ...

  4. Python selenium登录163邮箱示例

    思路:使用python自带的unittest单元测试框架测试163邮箱登录成功的case import unittestfrom selenium import webdriverimport tim ...

  5. python+selenium运行时,提示元素不可见

    python+selenium运行多次新增项目脚本(出错的元素通过by_id的方式定位),当第三次新增时报Message: element not visible的错误,加入等待时间,等页面加载完成, ...

  6. Python + Selenium 实现登录Office 365

    最近捡起之前用的Python + Selenium实现工作中需要的登录Office 365功能.(吐槽:国内网络真是卡,登录Office 365实属不易.另外Selenium这样的网站都要墙,无法理解 ...

  7. Python Selenium Cookie 绕过验证码实现登录

    Python Selenium Cookie 绕过验证码实现登录 之前介绍过博客园的通过cookie 绕过验证码实现登录的方法.这里并不多余,会增加分析和另外一种方法实现登录. 1.思路介绍 1.1. ...

  8. python RSA加密解密及模拟登录cnblog

    1.公开密钥加密 又称非对称加密,需要一对密钥,一个是私人密钥,另一个则是公开密钥.公钥加密的只能私钥解密,用于加密客户上传数据.私钥加密的数据,公钥可以解密,主要用于数字签名.详细介绍可参见维基百科 ...

  9. python+selenium自动化登录dnf11周年活动界面领取奖励登录部分采坑总结[1]

    背景: Dnf的周年庆活动之一,游戏在6月22日 06:00~6月23日 06:00之间登陆过游戏后可以于6月25日 16:00~7月04日 06:00领取奖励 目标:连续四天自动运行脚本,自动领取所 ...

随机推荐

  1. JavaScript Array对象sort() 方法小结

    sort() 方法用于对数组的元素进行排序. 语法arrayObject.sort(sortfunction) 参数sortfunction 可选.规定排序顺序.必须是函数. 返回值对数组的引用.请注 ...

  2. The type String cannot be constructed. You must configure the container to supply this value.

    利用 Enterprise Library 5.0 Microsoft.Practices.EnterpriseLibrary.Common Microsoft.Practices.Enterpris ...

  3. struct和typedef struct彻底明白了

    struct和typedef struct 分三块来讲述: 1 首先://注意在C和C++里不同 在C中定义一个结构体类型要用typedef: typedef struct Student { int ...

  4. 【 2013 Multi-University Training Contest 4 】

    HDU 4632 Palindrome subsequence dp[x][y]表示区间[x,y]构成回文串的方案数. 若str[x]==str[y],dp[x][y]=dp[x+1][y]+dp[x ...

  5. Java String.split()小点

    java String.split(); 别的不说,单说其中一个问题,这个函数去切分空字符串时,得到的结果: public static void main(String[] args) {// St ...

  6. 打造基于CentOS7的xfce最简工作环境

    参考这里:http://blog.csdn.net/smstong/article/details/44802989没成功. 又参考这里http://jensd.be/125/linux/rhel/i ...

  7. java之ubuntu12.04 开发环境配制

    配置java开发环境,即安装jdk: 1.配置环境变量 ,更改/etc/profile文件:sudo gedit /etc/profile; 在文件最后加上如下几行(其实跟windows下的配置原理一 ...

  8. MVC教程相关

    本教程所有文章导航 本系列共10篇文章,翻译自Asp.Net MVC4 官方教程,由于本系列文章言简意赅,篇幅适中,从一个示例开始讲解,全文最终完成了一个管理影片的小系统,非常适合新手入门Asp.Ne ...

  9. 近期编程问题——read:resource temporarily unavailable

    EAGAIN错误 出现问题:read:resource temporarily unavailable 原因:这种错误一般出现宰非阻塞的socket编程中,资源暂时不可用. 我的解决方法:囧,后来改成 ...

  10. Birt使用总结

    把report放到其他服务器要重新建立Data Source ,这是配置,拷贝项目时不会同时拷贝 (1)在EXTJs中利用Report实现报表的刷新 Ext.getCmp("showview ...