selenium web控件交互(ActionChains)

官方文档:https://selenium-python.readthedocs.io/api.html

ActionChains

执行PC端的鼠标右键、单击、双击、拖拽等事件

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

链式写法:

ActionChains(driver).move_to_element(element).click(element).perform()

分步写法:

actions = ActionChains(driver)
actions.move_to_element(element)
actions.click(element)
actions.perform() 

示例

from time import sleep
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys


class TestActionChains:

    def setup(self):
        self.driver = webdriver.Chrome(executable_path='./chromedriver.exe')

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

    # 单击、双击、右键
    def test_click(self):
        self.driver.get('http://sahitest.com/demo/clicks.htm')
        self.driver.maximize_window()
        self.driver.implicitly_wait(3)

        click_me = self.driver.find_element(By.CSS_SELECTOR, '[value="click me"]')
        dbl_click_me = self.driver.find_element(By.CSS_SELECTOR, '[value="dbl click me"]')
        right_click_me = self.driver.find_element(By.CSS_SELECTOR, '[value="right click me"]')

        action = ActionChains(self.driver)
        action.click(click_me)
        action.double_click(dbl_click_me)
        action.context_click(right_click_me)
        action.perform()
        sleep(3)

    # 鼠标移动到某个元素上
    def test_move_element(self):
        self.driver.get("http://wwww.baidu.com")
        self.driver.maximize_window()
        self.driver.implicitly_wait(3)

        setting = self.driver.find_element(By.CSS_SELECTOR, '#s-usersetting-top')
        action = ActionChains(self.driver)
        action.move_to_element(setting)
        action.perform()
        sleep(3)

    # 拖拽
    def test_drag_drop(self):
        self.driver.get("http://sahitest.com/demo/dragDropMooTools.htm")
        self.driver.maximize_window()
        self.driver.implicitly_wait(3)

        dragger = self.driver.find_element(By.XPATH, '//*[@id="dragger"]')
        item1 = self.driver.find_element(By.XPATH, '/html/body/div[2]')
        item2 = self.driver.find_element(By.XPATH, '/html/body/div[3]')
        item3 = self.driver.find_element(By.XPATH, '/html/body/div[4]')

        action = ActionChains(self.driver)
        # action.drag_and_drop(dragger, item1).perform()
        # action.click_and_hold(dragger).release(item2).perform()
        action.click_and_hold(dragger).move_to_element(item3).release().perform()

        sleep(3)

    # 模拟按键
    def test_key(self):
        self.driver.get("http://sahitest.com/demo/label.htm")
        username = self.driver.find_element_by_xpath("/html/body/label[1]/input")
        username.click()
        action = ActionChains(self.driver)
        action.send_keys("xiaoming").pause(1)
        action.send_keys(Keys.SPACE).pause(1)
        action.send_keys("xiaohong").pause(1)
        action.send_keys(Keys.BACK_SPACE).pause(1)
        action.perform()
        sleep(3)

    # 复制粘贴
    def test_copy_paste(self):
        self.driver.get("http://sahitest.com/demo/label.htm")
        username1 = self.driver.find_element_by_xpath("/html/body/label[1]/input")
        username2 = self.driver.find_element_by_xpath("/html/body/label[2]/table/tbody/tr/td[2]/input")
        username1.send_keys("xiaoming")
        username1.send_keys(Keys.CONTROL, 'a')
        sleep(1)
        username1.send_keys(Keys.CONTROL, 'c')
        sleep(1)
        username2.send_keys(Keys.CONTROL, 'v')
        sleep(3)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值