用Selenium实现鼠标单击、双击、右击

2022-04-28  本文已影响0人  吱吱菌啦啦

官方文档:https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

class TestActionChains():
    def setup(self):
        self.driver = webdriver.Chrome()
        # 隐式等待
        self.driver.implicitly_wait(5)
        self.driver.get("https://sahitest.com/demo/clicks.htm")

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

    def test_click(self):
        """
        单击、双击、右击
        :return:
        """
        # 定位元素
        element_click = self.driver.find_element(By.XPATH, '/html/body/form/input[3]')
        element_double_click = self.driver.find_element(By.XPATH, '/html/body/form/input[2]')
        element_right_click = self.driver.find_element(By.XPATH, '/html/body/form/input[4]')
        # 调用ActionChains方法,生成一个动作
        action = ActionChains(self.driver)
        # 给动作添加方法
        action.click(element_click)
        action.double_click(element_double_click)
        action.context_click(element_right_click)
        # 调用perform()方法执行
        action.perform()
上一篇 下一篇

猜你喜欢

热点阅读