python3.6+selenium_鼠标事件

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/1/11 11:19
# @File : unittest_test9_2.py
'''
鼠标事件:鼠标移动
'''
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.action_chains import ActionChains
import unittest
import time

class ToolTipTest(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.implicitly_wait(30)
        self.driver.get("http://jqueryui.com/tooltip/")

    def test_tool_tip(self):
        frame_ele = self.driver.find_element_by_class_name('demo-frame')
        self.driver.switch_to.frame(frame_ele)
        #定位搜索框age
        age_filed = self.driver.find_element_by_id('age')
        #移动鼠标至搜索框,注意,鼠标移动后必须执行提交perform()功能
        ActionChains(self.driver).move_to_element(age_filed).perform()
        time.sleep(2)

        #等待
        tool_tip_ele = WebDriverWait(self.driver,10)\
            .until(expected_conditions.visibility_of_element_located((By.CLASS_NAME,'ui-helper-hidden-accessible')))

        #print(tool_tip_ele.text)

        # 鼠标右键
        ActionChains(self.driver).context_click(age_filed)
        time.sleep(2)
        #判断
        self.assertEqual('We ask for your age only for statistical purposes.',tool_tip_ele.text)

    def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    unittest.main(verbosity=2)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/1/11 15:37
# @File : unittest_test9_3.py
'''
鼠标事件:双击操作
'''
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import unittest
import time

class DoubleClickTest(unittest.TestCase):
    URL = 'http://api.jquery.com/dblclick/'
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.implicitly_wait(30)
        self.driver.get(self.URL)

    def test_tool_tip(self):
        frame = self.driver.find_element_by_tag_name('iframe')
        self.driver.switch_to.frame(frame)

        box = self.driver.find_element_by_tag_name('div')

        #验证box中颜色是blue,
        # rgba(0,0,255,1),rgba括号中前3个数字代表着 red green blue三种颜色的rgb值,0-255,
        # 最后一个是设定这个颜色的透明度即alpha值。范围从0到1,越接近1,代表透明度越低
        # value_of_css_property(),css属性值
        self.assertEqual("rgba(0, 0, 255, 1)",box.value_of_css_property('background-color'))
        #移动鼠标至span
        ActionChains(self.driver).move_to_element(self.driver.find_element_by_tag_name('span')).perform()
        #鼠标双击box
        ActionChains(self.driver).double_click(box).perform()
        time.sleep(2)
        #验证box中颜色是yellow
        self.assertEqual("rgba(255, 255, 0, 1)",box.value_of_css_property('background-color'))

    def tearDown(self):
        self.driver.close()

if __name__ == "__main__":
    unittest.main(verbosity=2)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/1/11 16:00
# @File : unittest_test9_4.py
'''
鼠标事件:drag_and_drop()方法时间鼠标拖放
'''
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import unittest
import time

class DragAndDropTest(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.implicitly_wait(30)
        self.driver.get('http://jqueryui.com/resources/demos/droppable/default.html')

    def test_drag_and_drop(self):
        source = self.driver.find_element_by_id('draggable')
        target = self.driver.find_element_by_id('droppable')
        #把source拖至target
        ActionChains(self.driver).drag_and_drop(source,target).perform()
        #验证拖放成功
        self.assertEqual('Dropped!',target.text)


    def tearDown(self):
        self.driver.quit()

if __name__ == "__main__":
    unittest.main(verbosity=2)

 

转载于:https://www.cnblogs.com/xiuxiu123456/p/10444410.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值