selenium-web控件交互进阶Actions

一.Actions

ActionChains:执行PC端鼠标的点击,双击,右键,拖拽等事件

TouchActions:模拟PC端和移动端(h5页面)的点击,滑动,拖拽,多点触控等多种手势

二.动作链接ActionChains

参考:7.2 行为链 - selenium-python中文文档 (python-selenium-zh.readthedocs.io)

1.原理:

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

2.基本用法:

  • 生成一个动作action = ActionChains(driver)
  • 动作添加方法1:action.方法1

调用perform()方法执行action.perform()

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



class TestAction:
    def setup(self):
        self.driver = webdriver.Chrome()
        # 隐式等待
        self.driver.implicitly_wait(5)
        self.driver.get("https://www.baidu.com/")
        self.driver.maximize_window()


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

    def test_baidu(self):
        self.driver.find_element(By.ID, "kw").send_keys("格沃兹测试学院")
        self.driver.find_element(By.ID, "su").click()
        el = self.driver.find_element_by_link_text("设置")
        action = ActionChains(self.driver)
        action.move_to_element(el)
        action.perform()
        sleep(3)
3.ActionChains方法列表
click(on_element=None) ——单击鼠标左键
click_and_hold(on_element=None) ——点击鼠标左键,不松开
context_click(on_element=None) ——点击鼠标右键
double_click(on_element=None) ——双击鼠标左键
drag_and_drop(source, target) ——拖拽到某个元素然后松开
drag_and_drop_by_offset(source, xoffset, yoffset) ——拖拽到某个坐标然后松开
key_down(value, element=None) ——按下某个键盘上的键
key_up(value, element=None) ——松开某个键
move_by_offset(xoffset, yoffset) ——鼠标从当前位置移动到某个坐标
move_to_element(to_element) ——鼠标移动到某个元素
move_to_element_with_offset(to_element, xoffset, yoffset) ——移动到距某个元素(左上角坐标)多少距离的位置
perform() ——执行链中的所有动作
release(on_element=None) ——在某个元素位置松开鼠标左键
send_keys(*keys_to_send) ——发送某个键到当前焦点的元素 eg:action.send_keys(Key.SPACE) 输入空格,基本上键盘上的操作都可以模拟
send_keys_to_element(element, *keys_to_send) ——发送某个键到指定元素

三、TouchActions

参考:Selenium API PY 文档 - selenium.webdriver.common.touch_actions (liushilive.github.io)

主要方法有:

  • double_tap(on_element)   双击
  • flick_element(on_element, xoffset, yoffset, speed)   从元素开始以指定的速度移动
  • long_press(on_element)   长按不释放
  • move(xcoord, ycoord)   移动到指定的位置
  • perform()   执行链中的所有动作
  • release(xcoord, ycoord)   在某个位置松开操作
  • scroll(xoffset, yoffset) #滚动到某个位置
  • scroll_from_element(on_element, xoffset, yoffset)   从某元素开始滚动到某个位置
  • tap(on_element)      单击
  • tap_and_hold(xcoord, ycoord)     某点按
    from selenium import webdriver
    from selenium.webdriver import ActionChains, TouchActions
    from selenium.webdriver.common.by import By
    from time import sleep
    
    
    class TestCeshiren:
        def setup(self):
            opt = webdriver.ChromeOptions()
            opt.add_experimental_option('w3c', False)
            self.driver = webdriver.Chrome(chrome_options=opt)
            # 隐式等待
            self.driver.implicitly_wait(5)
            self.driver.get("https://www.baidu.com")
            self.driver.maximize_window()
    
    
        def teardown(self):
            self.driver.quit()
    
        def test_TouchActions(self):
            el = self.driver.find_element(By.ID, "kw")
            el_search = self.driver.find_element(By.ID, "su")
            el.send_keys("selenium测试")
            action = TouchActions(self.driver)
            action.tap(el_search)
            action.perform()
            action.scroll_from_element(el, 0, 10000).perform()
            sleep(3)

备注:执行报错:

self = <selenium.webdriver.remote.errorhandler.ErrorHandler object at 0x000002537D0AF7F0>
response = {‘status’: 404, ‘value’: ‘{“value”:{“error”:“unknown command”,“message”:“unknown command: Cannot call non W3C standard…jectPath [0x778387A4+228]\n\tRtlGetAppContainerNamedObjectPath [0x77838774+180]\n\t(No symbol) [0x00000000]\n”}}’}

解决:

设置下这个参数

opt = webdriver.ChromeOptions()
opt.add_experimental_option('w3c',  False)
driver = webdriver.Chrome(chrome_options=opt)

因为当前响应不是标准的W3C规范,所以无法处理,关闭W3C验证就可以了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值