Web自动化 -- 高级控件交互方法

ActionChains

selenium做自动化,有时候会遇到需要模拟鼠标操作才能进行的情况,比如单击、双击、点击鼠标右键、拖拽等等。而selenium给我们提供了一个类来处理这类事件——ActionChains

官方文档:

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

 

调用action chain方法时不会立即执行,而是按顺序放入一个队列,当调用perform方法时,会依次执行。

 

 

 

 代码案例1:
打开一个页面

分别对按钮执行点击,双击,右键操作

打印上面展示框的内容

class TestActionChains():
    def setup(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(3)
    def teardown(self):
        self.driver.quit()

    def test_case(self):
        self.driver.get("https://sahitest.com/demo/clicks.htm")
        ele_click = self.driver.find_element(By.XPATH,"//input[@value='click me']")  #定位到单击按钮
        ele_doubleclick = self.driver.find_element(By.XPATH,"//input[@value='dbl click me']") #定位到双击按钮
        ele_rightclick = self.driver.find_element(By.XPATH,"//input[@value='right click me']") #定位到右键按钮
        action = ActionChains(self.driver) #创建一个action
        action.click(ele_click)
        action.double_click(ele_doubleclick)
        action.context_click(ele_rightclick) #进行每个操作
        action.perform() #执行操作

        print(self.driver.find_element(By.XPATH,"//*[@name='t2']").get_attribute('value')) #打印出显示框的值

 

代码案例2:

将光标移动到某个元素上,并展示该内容的子项

def test_hold(self):
    self.driver.get("https://www.baidu.com/?tn=49055317_3_hao_pg")
    ele_search = self.driver.find_element(By.XPATH,"//*[@id='s-usersetting-top']")
    action = ActionChains(self.driver)
    action.move_to_element(ele_search)
    action.perform()
    time.sleep(3)

代码案例3:
点击某一项元素并拖拽到另一个元素上

def test_drag(self):
    self.driver.get("https://sahitest.com/demo/dragDropMooTools.htm")
    ele_drag = self.driver.find_element(By.ID,"dragger")
    ele_drop = self.driver.find_element(By.XPATH,"/html/body/div[2]")

    action = ActionChains(self.driver)
    action.drag_and_drop(ele_drag,ele_drop)
    #还可以用click_and_hold(ele_drag).move_to_element(ele_drop).release().perform()
    action.perform()

    time.sleep(3)

 代码案例4:

在输入框输入字符并使用键盘删除按键删除

def test_input(self):
    self.driver.get("https://sahitest.com/demo/label.htm")
    ele_input = self.driver.find_element(By.XPATH,"/html/body/label[1]/input")
    ele_input.click()

    action = ActionChains(self.driver)
    action.send_keys("username").pause(1)
    action.send_keys(Keys.SPACE)
    action.send_keys("TOM TMTM").pause(1)
    action.send_keys(Keys.BACKSPACE)
    action.perform()
    time.sleep(3)

 

TouchAction

ActionChains只是针对PC端程序鼠标模拟的一系列操作,对H5页面操作无效的。TouchAction可以对H5页面操作,通过TouchAction可以实现点击,滑动,拖拽,多点触控,以及模拟手势的各种操作

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

 

进入百度网站,搜索,然后滚动查看结果

 

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


class TestTouchAction():
    def setup(self):
        option = webdriver.ChromeOptions()
        option.add_experimental_option('w3c',False)
        self.driver = webdriver.Chrome(options=option)
        self.driver.maximize_window()
        self.driver.implicitly_wait(3)
    def teardown(self):
        self.driver.quit()

    def test_search_scroll(self):
        self.driver.get("https://www.baidu.com/")
        ele = self.driver.find_element(By.ID,'kw')
        ele_button = self.driver.find_element(By.ID, 'su')
        ele.send_keys("selenium")

        action = TouchActions(self.driver)
        action.tap(ele_button)
        action.perform()

        action.scroll_from_element(ele,0,1000)
        action.perform()

表单的操作

和普通的定位点击方法一致

 

可参考:

https://blog.csdn.net/gjj920318/article/details/120565475

posted @   lms21  阅读(148)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示