美文网首页
selenium中ActionChains的简单使用

selenium中ActionChains的简单使用

作者: 金融测试民工 | 来源:发表于2020-08-10 16:02 被阅读0次

selenium-python版本官方文档地址:https://selenium-python.readthedocs.io/

selenium-WebDriver API地址:https://selenium-python.readthedocs.io/api.html

一、前言

selenium中ActionChains可以用来执行PC端的鼠标点击,双击,右键点击、拖拽等事件,是selenium模拟对键盘、鼠标操作的工具。

二、执行原理

调用ActionChains的方法时,不会立即执行,而是将所有的操作,按顺序存放在一个队列中,当调用perform()方法时,队列中的事件会依次执行。

三、演示

1.单击、双击、右键点击:http://sahitest.com/demo/clicks.htm

2.移动鼠标到某个元素上:https://www.baidu.com/

3.将一个元素拖拽到另一个元素上:http://sahitest.com/demo/dragDropMooTools.htm

4.键盘空格、回退键演示:http://sahitest.com/demo/label.htm

代码如下:

1.base.py文件:

# -*- coding:utf-8 -*-

# @File:base.py

import pytest

from selenium.webdriver import ActionChains

from time import sleep

from selenium.webdriver.common.keys import Keys

from selenium_test.base import Base

import allure

class TestActionChains(Base):

    @pytest.mark.skip

    @allure.feature("单击、双击、右键点击")

    def test_case_click(self):

        self.driver.get("http://sahitest.com/demo/clicks.htm")

        element_click = self.driver.find_element_by_xpath('/html/body/form/input[3]')

        element_doubleclick = self.driver.find_element_by_xpath('/html/body/form/input[2]')

        element_rightclick = self.driver.find_element_by_xpath('/html/body/form/input[4]')

        action = ActionChains(self.driver)

        action.click(element_click)

        action.double_click(element_doubleclick)

        action.context_click(element_rightclick)

        sleep(3)

        action.perform()

        sleep(3)

    @pytest.mark.skip

    @allure.feature("移动鼠标到某个元素上")

    def test_movetoelement(self):

        self.driver.get("https://www.baidu.com/")

        ele = self.driver.find_element_by_xpath('//*[@id="s-usersetting-top"]')

        action = ActionChains(self.driver)

        action.move_to_element(ele)

        action.perform()

        sleep(3)

    @pytest.mark.skip

    @allure.feature("将一个元素拖动到另一个元素上")

    def test_draganddrop(self):

        self.driver.get("http://sahitest.com/demo/dragDropMooTools.htm")

        source_element = self.driver.find_element_by_id('dragger')

        target_element1 = self.driver.find_element_by_xpath('/html/body/div[2]')

        target_element2 = self.driver.find_element_by_xpath('/html/body/div[3]')

        target_element3 = self.driver.find_element_by_xpath('/html/body/div[4]')

        target_element4 = self.driver.find_element_by_xpath('/html/body/div[5]')

        action = ActionChains(self.driver)

        # action.drag_and_drop(source_element,target_element1)

        # action.drag_and_drop(source_element,target_element2)

        # action.drag_and_drop(source_element,target_element3)

        # action.drag_and_drop(source_element,target_element4)

        # action.click_and_hold(source_element).release(target_element1)                    # 方式2

        action.click_and_hold(source_element).move_to_element(target_element1).release()    # 方式3

        action.perform()

        sleep(3)

    @allure.feature("输入username tom,并删除一个m")

    def test_keys(self):

        self.driver.get("http://sahitest.com/demo/label.htm")

        ele = self.driver.find_element_by_xpath('/html/body/label[1]/input')

        action = ActionChains(self.driver)

        action.click(ele)

        action.send_keys("username").pause(1)

        action.send_keys(Keys.SPACE).pause(1)

        action.send_keys("tom").pause(1)

        action.send_keys(Keys.BACK_SPACE).perform()

        sleep(3)

相关文章

网友评论

      本文标题:selenium中ActionChains的简单使用

      本文链接:https://www.haomeiwen.com/subject/otyjdktx.html