zoukankan      html  css  js  c++  java
  • python+selenium实例: 将QQ网站上的新闻转载到自己的网站

    #coding: utf-8
    import unittest
    from selenium import webdriver
    import time

    class LoginCase(unittest.TestCase):

    def setUp(self): #每个用例执行之前执行
    print 'before test'
    self.dr = webdriver.Chrome()
    # self.dr.get('http://localhost/wordpress/wp-login.php')

    def test_homework_qq_today(self):
    title_and_content = self.get_qq_today()
    title = title_and_content[0]
    content = title_and_content[1]

    self.dr.get('http://localhost/wordpress/wp-login.php')
    user_name = password = 'admin'
    self.login(user_name, password)

    self.dr.get('http://localhost/wordpress/wp-admin/post-new.php')
    self.by_name('post_title').send_keys(title)
    self.set_content_with_html(content)
    self.by_name('publish').click()

    def get_qq_today(self):
    title_and_content = []
    self.dr.get('http://www.qq.com/')
    time.sleep(2)
    qq_today_link = self.by_css('#todaytop a')
    title_and_content.append(qq_today_link.text)
    href = qq_today_link.get_attribute('href')
    print(href)
    self.dr.get(href)
    time.sleep(2)

    title_and_content.append(self.by_id('articleContent').get_attribute('innerHTML'))
    print(title_and_content)
    return title_and_content

    def set_content(self, text):
    js = "document.getElementById('content_ifr').contentWindow.document.body.innerText = '%s'" %(text)
    print js
    self.dr.execute_script(js)

    def set_content_with_html(self, html):
    html = html.replace(" ", ' ')
    js = "document.getElementById('content_ifr').contentWindow.document.body.innerHTML = '%s'" %(html)
    print js
    self.dr.execute_script(js)

    def login(self, user_name, password):
    self.by_id('user_login').send_keys(user_name)
    self.by_id('user_pass').send_keys(password)
    self.by_id('wp-submit').click()

    def by_id(self, the_id):
    return self.dr.find_element_by_id(the_id)

    def by_css(self, css):
    return self.dr.find_element_by_css_selector(css)

    def by_name(self, name):
    return self.dr.find_element_by_name(name)

    def tearDown(self): #每个用例执行之后
    print 'after every test'
    self.dr.quit()

    if __name__ == '__main__':
    unittest.main()

  • 相关阅读:
    matplotlib: ylabel on the right
    ssh 密钥管理
    [转]Linux下创建静态、动态库
    Perl命令行应用介绍
    zz:快速编辑Shell命令行
    zz Makefile学习教程: 跟我一起写 Makefile
    Eureka服务剔除下线
    数据结构可视化
    aeImageResize jQuery图片等比缩放调整插件
    最全的CSS浏览器兼容问题整理(IE6.0、IE7.0 与 FireFox)
  • 原文地址:https://www.cnblogs.com/bzdmz/p/10631054.html
Copyright © 2011-2022 走看看