import timefrom selenium import webdriverfrom selenium.webdriver import TouchActionsfrom selenium.webdriver.common.by import By
driver = webdriver.Chrome()driver.implicitly_wait(5)driver.get('https://www.baidu.com/')
actions = TouchActions(driver)ele_input = driver.find_element(By.ID, "kw")location = ele_input.location'''ActionChains格式,使用方法与ActionChains一致:1. 实例化一个TouchActions动作容器actions = TouchActions(driver)
2. 往动作容器添加动作2.1 直接添加actions.tap(ele_click)                              # 调用的动作都会添加到动作容器中actions.double_tap(ele_drag).scroll(0, 1000)        # 链式添加动作。每个动作返回值为容器对象,因此支持链式连续添加
3. 执行动作actions.perform()'''
# 鼠标点击actions.tap(ele_input)                          # 单击元素actions.double_tap(ele_input)                   # 双击元素actions.long_press(ele_input)                   # 长按元素
# 鼠标滚动x, y = 30, 50actions.scroll(x, y)                            # 滚动,向下向右(x, y)距离actions.scroll_from_element(ele_input, x, y)    # 滚动,以元素为起点向下向右(x, y)距离
# 鼠标移动x, y = 0, 1000actions.move(x, y)                              # 鼠标移动到坐标(x,y)
# 释放鼠标actions.release(x, y)                           # 鼠标移动坐标(x,y),并释放鼠标
# 滑动xspeed, yspeed = 30, 50actions.flick(xspeed, yspeed)                               # 滑动,向下向右以(xspeed, yspeed)速度滑动
xoffset, yoffset, speed = 30, 50, 20actions.flick_element(ele_input, xoffset, yoffset, speed)   # 在元素处滑动,向下向右以speed的速度滑动(xoffset, yoffset)距离
# 拖动x, y = 30, 50actions.tap_and_hold(x, y)                      # 点住,在坐标(x, y)处
# 容器相关actions.perform()                               # 按顺序开始执行动作
time.sleep(5)driver.quit()
评论