07-1-selenium的模拟鼠标操作(ActionChains)

selenium的模拟鼠标操作(ActionChains)

在日常的测试中,经常会遇到需要鼠标去操作的一些事情,比如说悬浮菜单、拖动验证码等,之前用webelement的方法可进行简单鼠标操作,这一节讲动作链,来学习如何使用webdriver模拟鼠标的操作
模拟鼠标的操作要首先从webdriver.common引入ActionChains的包

from selenium.webdriver.common.action_chains import ActionChains

d247f3fc278d7d980a73aa38e2ea1898.png

一、ActionChains的常用操作

  • click(element):单击

  • context_click(element) : 右击element元素

  • double_click(element):双击element元素

  • move_by_offset(xoffset,yoffset):移动鼠标到指定的x,y位置(相对于浏览器的绝对位置)

  • move_to_element_with_offset(element, xoffset, yoffset):相对element元素,移动鼠标到指定的x,y位置(相对于element元素的相对位置)

  • click_and_hold(element1=None):在element1元素上按下鼠标左键,并保持按下动作(元素默认为空)

  • release(element2=None): 在element2元素上松开鼠标左键(元素默认为空)

  • key_down(key , element1=None):在element1元素上,按下指定的键盘key(ctrl、shift等)键,并保持按下动作(元素默认为空)

  • key_up(key , element2=None):在element2元素上,松开指定的键盘key(元素默认为空)

  • send_keys(key): 向当前定位元素发送某个key键

  • send_keys_to_element(element ,key):向element元素发送某个key键

1.1鼠标拖动操作(滑动验证码问题)

方法:

  • drag_and_drop(self, source, target)

source:鼠标拖动的原始元素

target:鼠标拖动到的另外一个元素(的位置)

拖动source元素到target元素的位置

  • drag_and_drop_by_offset(self, source, xoffset, yoffset)

source:鼠标拖动的原始元素

xoffset:鼠标把元素拖动到另外一个位置的x坐标偏移量

yoffset:鼠标把元素拖动到另外一个位置的y坐标偏移量

拖动source元素到指定的坐标

左上角为原点
'''x坐标为正数向右偏移,x坐标为负数向左偏移'''
'''y坐标为正数向下偏移,y坐标为负数向上偏移'''

代码示例

我们用淘宝的注册页面案例来说明鼠标拖动操作:把滑块从左端移到右端。

fc23c656a68478bb6492045bb4d9924b.png

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

driver=webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("http://www.taobao.com")

#点击首页免费注册
reg_ele=driver.find_element_by_xpath("//a[text()='免费注册']")
ActionChains(driver).click(reg_ele).perform()

#关闭div,点击同意协议
div_ele=driver.find_element_by_xpath("//button[text()='同意协议']")
ActionChains(driver).click(div_ele).perform()

#获取滑动条size
ban_ele=driver.find_element_by_xpath("//span[@class='nc-lang-cnt' and contains(text(),'按住')]")
ban_size=ban_ele.size#返回字典{'height': 32, 'width': 300}

#获取滑块位置
block_ele=driver.find_element_by_xpath("//span[@id='nc_1_n1z']")
#拖拽移动
x_location=ban_size['width']
y_location=0
ActionChains(driver).drag_and_drop_by_offset(block_ele,x_location,y_location).perform()

代码没有问题,但是 滑动滑块后却显示:哎呀,出错了,点击刷新再来一次(error:1M46j)

知乎大神有讲解,里面对于模拟登录失败的原因通俗易懂的做了相关分析:

因此,在web端模拟登陆可以得出结论:淘宝对于模拟登录抓取cookie的行为动作进行了一些判断,我们无法使用脚本来代替人为的操作,此次尝试失败

1.2 鼠标悬浮操作

move_to_element(element) :鼠标移动(悬浮)到某个元素之上

element,要悬浮的元素

代码示例

百度首页,移到设置悬浮,点击高级搜索

import time

from selenium import webdriver
from selenium.webdriver import ActionChains

driver = webdriver.Chrome()

driver.implicitly_wait(10)

driver.get("http://www.baidu.com")

# 鼠标悬停到设置
action = ActionChains(driver)
# 定位元素
setting_elem = driver.find_element_by_xpath("//a[@name='tj_settingicon' and @href='http://www.baidu.com/gaoji/preferences.html']")
action.move_to_element(setting_elem)
# 释放动作
action.perform()

# 点击高级搜索 //a[text()="高级搜索"]
gaoji_setting = driver.find_element_by_xpath('//a[text()="高级搜索"]')
action.click(gaoji_setting)
# 释放动作
action.perform()

# action.click().move_to_element().context_click().drag_and_drop().perform()

time.sleep(2)
原文地址:https://www.cnblogs.com/testeremma/p/12684537.html