用python+selenium登录cnblog后新增文章后再次删除该文章

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

代码如下:

 1 #coding: utf-8
 2 from selenium import webdriver
 3 from time import sleep
 4 import unittest
 5 import time
 6 
 7 class DeletePost(unittest.TestCase):
 8 
 9     def setUp(self):
10         self.dr = webdriver.Chrome()
11         self.dr.maximize_window()
12 
13     #定义登录方法
14     def login(self, username, password):
15         self.dr.get('https://passport.cnblogs.com/user/signin')  #cnblog登录页面
16         self.dr.find_element_by_id('input1').send_keys(username)
17         self.dr.find_element_by_id('input2').send_keys(password)
18         self.dr.find_element_by_id('signin').click()
19 
20     #定义新增文章方法
21     def create_post(self, title, content):
22         self.dr.get('https://i.cnblogs.com/EditPosts.aspx?opt=1')  #cnblog新增文章页面
23         self.dr.find_element_by_id('Editor_Edit_txbTitle').send_keys(title)
24         self.set_content(content)
25         self.dr.find_element_by_id('Editor_Edit_lkbPost').click()
26 
27     #定义输入富文本content方法
28     def set_content(self, content):
29         js = 'document.getElementById("Editor_Edit_EditorBody_ifr").contentWindow.document.body.innerHTML=\'%s\'' % (content)
30         self.dr.execute_script(js)
31 
32     #定义获取文章post-id方法
33     def create_post_and_return_its_id(self, title, content):
34         self.create_post(title, content)
35         tokens = self.dr.find_element_by_css_selector('#TipsPanel_LinkEdit').get_attribute('href').split('=')
36         return tokens[-1]  #所有文章都对应唯一的post-id
37 
38     #验证删除新增的文章
39     def test_delete_post_success(self):
40         '''验证删除新增加的Post'''
41         self.login('kemi_xxxx', 'kemi_xxxx') #cnblog帐号密码
42         title = 'title %s' %(time.time())  #标题为title和当前时间
43         content = 'content %s' %(time.time())  #内容为content和当前时间
44         sleep(5)
45         post_id = self.create_post_and_return_its_id(title, content) #调用post-id方法并获得相应id
46         self.dr.get('https://i.cnblogs.com/')  #cnblog后台管理页面
47         row_id = 'post-row-' + post_id  #定义文章列表的行id
48         post = self.dr.find_element_by_id(row_id)  #定位到相应行id上
49         post.find_element_by_xpath("//a[@href='javascript:void(0)']").click()  #定位删除并点击
50         self.dr.switch_to_alert().accept()  #点击弹窗中的确定
51         sleep(2)
52         post.find_element_by_xpath("//span[@style='color:red']")  #定位相应post的删除成功!提示
53 
54     def tearDown(self):
55         print('测试完毕!')
56         self.dr.quit()
57 
58 if __name__ == '__main__':
59     unittest.main()

 

效果如下:

 

posted @ 2016-12-27 10:07  菜鸟可米  阅读(762)  评论(0)    收藏  举报
jQuery火箭图标返回顶部代码