Selenium笔记(5)动作链

本文集链接:https://www.jianshu.com/nb/25338984

简介

一般来说我们与页面的交互可以使用Webelement的方法来进行点击等操作。但是,有时候我们需要一些更复杂的动作,类似于拖动,双击,长按等等。

这时候就需要用到我们的Action Chains(动作链)了。

简例

from selenium.webdriver import ActionChains

element = driver.find_element_by_name("source")
target = driver.find_element_by_name("target")

actions = ActionChains(driver)
actions.drag_and_drop(element, target)
actions.perform()

在导入动作链模块以后,需要声明一个动作链对象,在声明时将webdriver当作参数传入,并将对象赋值给一个actions变量。

然后我们通过这个actions变量,调用其内部附带的各种动作方法进行操作。

注:在调用各种动作方法后,这些方法并不会马上执行,而是会按你代码的顺序存储在ActionChains对象的队列中。当你调用perform()时,这些动作才会依次开始执行。

常用动作方法

  • click(on_element=None)

    左键单击传入的元素,如果不传入的话,点击鼠标当前位置。

  • context_click(on_element=None)

    右键单击。

  • double_click(on_element=None)

    双击。

  • click_and_hold(on_element=None)

    点击并抓起

  • drag_and_drop(sourcetarget)

    在source元素上点击抓起,移动到target元素上松开放下。

  • drag_and_drop_by_offset(sourcexoffsetyoffset)

    在source元素上点击抓起,移动到相对于source元素偏移xoffset和yoffset的坐标位置放下。

  • send_keys(*keys_to_send)

    将键发送到当前聚焦的元素。

  • send_keys_to_element(element*keys_to_send)

    将键发送到指定的元素。

  • reset_actions()

    清除已经存储的动作。

完整文档

class selenium.webdriver.common.action_chains.``ActionChains(driver)

Bases: object

ActionChains are a way to automate low level interactions such as mouse movements, mouse button actions, key press, and context menu interactions. This is useful for doing more complex actions like hover over and drag and drop.

Generate user actions.

When you call methods for actions on the ActionChains object, the actions are stored in a queue in the ActionChains object. When you call perform(), the events are fired in the order they are queued up.

ActionChains can be used in a chain pattern:

menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")

ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()

Or actions can be queued up one by one, then performed.:

menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")

actions = ActionChains(driver)
actions.move_to_element(menu)
actions.click(hidden_submenu)
actions.perform()

Either way, the actions are performed in the order they are called, one after another.

  • __init__(driver)

    Creates a new ActionChains.

    Args:

    • driver: The WebDriver instance which performs user actions.

  • click(on_element=None)

    Clicks an element.

    Args:

    • on_element: The element to click. If None, clicks on current mouse position.

  • click_and_hold(on_element=None)

    Holds down the left mouse button on an element.

    Args:

    • on_element: The element to mouse down. If None, clicks on current mouse position.

  • context_click(on_element=None)

    Performs a context-click (right click) on an element.

    Args:

    • on_element: The element to context-click. If None, clicks on current mouse position.

  • double_click(on_element=None)

    Double-clicks an element.

    Args:

    • 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.

    Args:

    • source: The element to mouse down.

    • target: The element to mouse up.

  • drag_and_drop_by_offset(source, xoffset, yoffset)

    Holds down the left mouse button on the source element,then moves to the target offset and releases the mouse button.

    Args:

    • source: The element to mouse down.

    • xoffset: X offset to move to.

    • yoffset: Y offset to move to.

  • key_down(value, element=None)

    Sends a key press only, without releasing it.Should only be used with modifier keys (Control, Alt and Shift).

    Args:

    • value: 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.

    Example, pressing ctrl+c:

    ActionChains(driver).key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform() 
  • key_up(value, element=None)

    Releases a modifier key.

    Args:

    • value: 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.

    Example, pressing ctrl+c:

    ActionChains(driver).key_down(Keys.CONTROL).send_keys('c').key_up(Keys.CONTROL).perform()
  • move_by_offset(xoffset, yoffset)

    Moving the mouse to an offset from current mouse position.

    Args:

    • xoffset: X offset to move to, as a positive or negative integer.

    • yoffset: Y offset to move to, as a positive or negative integer.

  • move_to_element(to_element)

    Moving the mouse to the middle of an element.

    Args:

    • to_element: The WebElement to move to.

  • move_to_element_with_offset(to_element, xoffset, yoffset)

    Move the mouse by an offset of the specified element.Offsets are relative to the top-left corner of the element.

    Args:

    • to_element: The WebElement to move to.

    • xoffset: X offset to move to.

    • yoffset: Y offset to move to.

  • pause(seconds)

    Pause all inputs for the specified duration in seconds

  • perform()

    Performs all stored actions.

  • release(on_element=None)

    Releasing a held mouse button on an element.

    Args:

    • on_element: The element to mouse up. If None, releases on current mouse position.

  • reset_actions()

    Clears actions that are already stored on the remote end.

  • send_keys(*keys_to_send)

    Sends keys to current focused element.

    Args:

    • keys_to_send: The keys to send. Modifier keys constants can be found in the ‘Keys’ class.

  • send_keys_to_element(element, *keys_to_send)

    Sends keys to an element.

    Args:

    • element: The element to send keys.

    • keys_to_send: The keys to send. Modifier keys constants can be found in the ‘Keys’ class.

python爬虫基础11-selenium大全5/8-动作链的更多相关文章

  1. python爬虫-基础入门-python爬虫突破封锁

    python爬虫-基础入门-python爬虫突破封锁 >> 相关概念 >> request概念:是从客户端向服务器发出请求,包括用户提交的信息及客户端的一些信息.客户端可通过H ...

  2. Python 爬虫的工具列表大全

    Python 爬虫的工具列表大全 这个列表包含与网页抓取和数据处理的Python库.网络 通用 urllib -网络库(stdlib). requests -网络库. grab – 网络库(基于pyc ...

  3. Python爬虫基础

    前言 Python非常适合用来开发网页爬虫,理由如下: 1.抓取网页本身的接口 相比与其他静态编程语言,如java,c#,c++,python抓取网页文档的接口更简洁:相比其他动态脚本语言,如perl ...

  4. python爬虫动态html selenium.webdriver

    python爬虫:利用selenium.webdriver获取渲染之后的页面代码! 1 首先要下载浏览器驱动: 常用的是chromedriver 和phantomjs chromedirver下载地址 ...

  5. python爬虫-基础入门-爬取整个网站《3》

    python爬虫-基础入门-爬取整个网站<3> 描述: 前两章粗略的讲述了python2.python3爬取整个网站,这章节简单的记录一下python2.python3的区别 python ...

  6. python爬虫-基础入门-爬取整个网站《2》

    python爬虫-基础入门-爬取整个网站<2> 描述: 开场白已在<python爬虫-基础入门-爬取整个网站<1>>中描述过了,这里不在描述,只附上 python3 ...

  7. python爬虫-基础入门-爬取整个网站《1》

    python爬虫-基础入门-爬取整个网站<1> 描述: 使用环境:python2.7.15 ,开发工具:pycharm,现爬取一个网站页面(http://www.baidu.com)所有数 ...

  8. Python爬虫之设置selenium webdriver等待

    Python爬虫之设置selenium webdriver等待 ajax技术出现使异步加载方式呈现数据的网站越来越多,当浏览器在加载页面时,页面上的元素可能并不是同时被加载完成,这给定位元素的定位增加 ...

  9. 爬虫04 /asyncio、selenium规避检测、动作链、无头浏览器

    爬虫04 /asyncio.selenium规避检测.动作链.无头浏览器 目录 爬虫04 /asyncio.selenium规避检测.动作链.无头浏览器 1. 协程asyncio 2. aiohttp ...

  10. selenium处理iframe和动作链

    selenium处理iframe和动作链 iframe iframe就是一个界面里嵌套了其他界面,这个时候selenium是不能从主界面找到子界面的属性,需要先找到子界面,再去找子界面的属性 动作链( ...

随机推荐

  1. c++笔记3

    一基本语法: 1.1 字符串:支持标准C的 const char* pch=0/"";//不指向任何对象和指向空字符串.C++提供的string类可提供字符串的所有操作,最好是融合 ...

  2. Linux Ubuntu系统之PPP拨号经验分享

    近期,工作需要,我负责开发PPP拨号模块. 说起拨号,算算时间,我已经做过2次了, 暴露年龄了,呵呵. 第一次是刚毕业做的PPOE拨号,给电信做拨号软件,在河北石家庄工作过一段时间,基于windows ...

  3. LCA 离线做法tarjan

    tarjan(int u) { int v; for(int i=h[u];i;i=nex[i])//搜索边的 { v=to[i]; tarjan(v); marge(u,v); vis[v]=; } ...

  4. MyBatis配置文件之概述

    MyBatis配置文件所有元素 <?xml version="1.0" encoding="UTF-8"?> <configuration&g ...

  5. JAVA基础之线程

    个人理解: 在相同的进程也就是运行同样的程序的前提下,线程越多效率越快!当然硬件也是个障碍!为了提高效率,可以多创建线程,但是也不是越多越好,这就需要了线程池进行管理!需要知道的线程实现的方法:继承T ...

  6. jQuery知识点小结

    博主之前学习一段时间后做了点Demo,借此机会发出来分享,其实学jQuery只要简单看看文档即可,但有些细枝末节的东西文档会默认使用者是了解的,所以还是得系统学习系统训练:Talk is cheap, ...

  7. Spring 整合 Quartz 实现动态定时任务(附demo)

    最近项目中需要用到定时任务的功能,虽然Spring 也自带了一个轻量级的定时任务实现,但感觉不够灵活,功能也不够强大.在考虑之后,决定整合更为专业的Quartz来实现定时任务功能. 普通定时任务 首先 ...

  8. 【javascript类库】zepto和jquery的md5加密插件

    [javascript类库]zepto和jquery的md5加密插件 相信很多人对jQuery并不陌生,这款封装良好的插件被很多开发者使用. zepto可以说是jQuery在移动端的替代产品,它比jQ ...

  9. AutoIt 脚本1

    一.新建的AU3 脚本进行编辑 选择Edit Script 如果是相运行脚本可以用Run Script 如果是想将脚本编译成.exe 可以用Compile Script 1)一个简单的AU3脚本 Ma ...

  10. Netweaver和SAP云平台的quota管理

    Netweaver 以需要为一个用户上下文(User Context)能够在SAP extended memory区域中分配内存尺寸创建quota为例. 对于Dialog工作进程,使用事务码修改参数 ...