zoukankan      html  css  js  c++  java
  • python+selenium实例: 鼠标移动到某行记录,拼接,并执行相关的操作,ActionChains perform的使用

    #coding: utf-8
    import unittest
    from selenium import webdriver
    import time
    from selenium.webdriver.common.action_chains import ActionChains

    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_delete_post(self):
    user_name = password = 'admin'
    self.login(user_name, password)

    content = title = 'this is title %s' %(time.time())
    post_id = self.create_post_and_return_its_id(title, content)
    self.dr.get('http://localhost/wordpress/wp-admin/edit.php')
    row_id = 'post-' + post_id
    the_row = self.by_id(row_id)

    ActionChains(self.dr).move_to_element(the_row).perform()
    the_row.find_element_by_css_selector('.submitdelete').click()

    self.assertNotEqual(self.by_css('.row-title').text, title)

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

    def create_post_and_return_its_id(self, title, content):
    self.create_post(title, content)
    link_text = self.by_id('sample-permalink').text
    tokens = link_text.split('=')
    return tokens[-1]


    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()

  • 相关阅读:
    NodeJS优缺点
    移动端触摸相关事件touch、tap、swipe
    vscode使用技巧
    js 字符串转数字
    js 导出Excel
    <!--[if IE 9]> <![endif]-->
    js 异步请求
    关于windows串口处理
    mfc 托盘提示信息添加
    微软的麦克风处理示列代码
  • 原文地址:https://www.cnblogs.com/bzdmz/p/10631382.html
Copyright © 2011-2022 走看看