设为首页 收藏本站
查看: 968|回复: 0

[经验分享] selenium-webdriver(python) (十五)

[复制链接]

尚未签到

发表于 2015-4-22 09:37:53 | 显示全部楼层 |阅读模式
本节重点:
ActionChains 类


  •   context_click()  右击
  •   double_click()   双击
  •   drag_and_drop()  拖动


测试的产品中有一个操作是右键点击文件列表会弹出一个快捷菜单,可以方便的选择快捷菜单中的选择对文件进行操作(删除、移动、重命名),之前学习元素的点击非常简单:
driver.find_element_by_id(“xxx”).click()
那么鼠标的双击、右击、拖动等是否也是这样的写法呢?例如右击:
driver.find_element_by_id(“xxx”).context_click()
经过运行脚本得到了下面的错误提示:
AttributeError: 'WebElement' object has no attribute 'context_click'
提示右点方法不属于webelement 对象,通过查找文档,发现属于ActionChains 类,但文档中没有具体写法。这里要感谢 北京-QC-rabbit 的指点,其实整个python+selenium 学习过程都要感谢 北京-QC-rabbit 的指点。


下面介绍鼠标右键的用法,以快播私有云为例:



#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Firefox()
driver.get("http://passport.kuaibo.com/login/?referrer=http%3A%2F%2Fwebcloud.kuaibo.com%2F")
#登陆快播私有云
driver.find_element_by_id("user_name").send_keys("username")
driver.find_element_by_id("user_pwd").send_keys("123456")
driver.find_element_by_id("dl_an_submit").click()
time.sleep(3)
#定位到要右击的元素
qqq =driver.find_element_by_xpath("/html/body/div/div[2]/div[2]/div/div[3]/table/tbody/tr/td[2]")
#对定位到的元素执行鼠标右键操作
ActionChains(driver).context_click(qqq).perform()

'''
#你也可以使用三行的写法,但我觉得上面两行写法更容易理解
chain = ActionChains(driver)
implement = driver.find_element_by_xpath("/html/body/div/div[2]/div[2]/div/div[3]/table/tbody/tr/td[2]")
chain.context_click(implement).perform()
'''
time.sleep(3) #休眠3秒
driver.close()
这里需要注意的是,在使用ActionChains 类之前,要先将包引入。


右击的操作会了,下面的其它方法比葫芦画瓢也能写出来。
鼠标双击的写法:



#定位到要双击的元素
qqq =driver.find_element_by_xpath("xxx")
#对定位到的元素执行鼠标双击操作
ActionChains(driver).double_click(qqq).perform()


鼠标拖放操作的写法:



#定位元素的原位置
element = driver.find_element_by_name("source")
#定位元素要移动到的目标位置
target =  driver.find_element_by_name("target")
#执行元素的移动操作
ActionChains(driver).drag_and_drop(element, target).perform()
  

ActionChains 类不仅仅是只包含了上面的三个方法,下面将方法列出:
class ActionChains(driver)
driver:The WebDriver instance which performs user actions.
Generate user actions. All actions are stored in the ActionChains object. Call perform() to fire stored actions.

  – perform()
Performs all stored actions.

  – click(on_element=None)
Clicks an element.
on_element:The element to click. If None, clicks on current mouse position.

  – click_and_hold(on_element)
Holds down the left mouse button on an element.
on_element:The element to mouse down. If None, clicks on current mouse position.

  – context_click(on_element)
Performs a context-click (right click) on an element.
on_element:The element to context-click. If None, clicks on current mouse position.

  – double_click(on_element)
Double-clicks an element.
on_element:The element to double-click. If None, clicks on current mouse position.
  
  – drag_and_drop(source, target)
Holds down the left mouse button on the source element, then moves to the target element and releases the mouse button.
source:The element to mouse down.
target: The element to mouse up.

  – key_down(key, element=None)
Sends a key press only, without releasing it. Should only be used with modifier keys (Control, Alt andShift).
key:The modifier key to send. Values are defined in Keys class.
element:The element to send keys. If None, sends a key to current focused element.
  – key_up(key, element=None)
Releases a modifier key.
key:The modifier key to send. Values are defined in Keys class.
element:The element to send keys. If None, sends a key to current focused element.

  – move_by_offset(xoffset, yoffset)
Moving the mouse to an offset from current mouse position.
xoffset:X offset to move to.yoffset:Y offset to move to.

  – move_to_element(to_element)
Moving the mouse to the middle of an element.
to_element: The element to move to.

  – move_to_element_with_offset(to_element, xoffset, yoffset)
Move the mouse by an offset of the specificed element. Offsets are relative to the top-left corner of the
element.
to_element: The element to move to.xoffset:X offset to move to.yoffset:Y offset to move to.

  – release(on_element)
Releasing a held mouse button.
on_element:The element to mouse up.

  – send_keys(*keys_to_send)
Sends keys to current focused element.
keys_to_send:The keys to send.

  – send_keys_to_element(self, element,*keys_to_send):
Sends keys to an element.
element:The element to send keys.keys_to_send:The keys to send.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-59496-1-1.html 上篇帖子: python脚本实现集群检测和管理 下篇帖子: python 函数的*args,**kwargs
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表