zoukankan      html  css  js  c++  java
  • Selenium之ActionChains模拟用户行为

    1、需求:需要模拟鼠标操作才能进行的情况,比如单击、双击、鼠标右键、拖拽等操作

    2、解决办法:selenium 提供了一个类来处理这类事件:

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

    3、引用:from selenium.webdriver.common.action_chains import AchtionChains

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

    5、写法:支持链式写法和分步写法

    ActionChains(driver).click(ele).perform()

    6、键盘和鼠标方法列表:

    (1)perform()执行列表中所有的操作

    (2)click(on_element=None)  单击鼠标左键

    (3)context_click(on_element=None)  单击鼠标右键

    (4)double_click(on_element=None)  双击鼠标左键

    (5)move_to_element(to_element)  移动鼠标到某个元素上

    (6)ele.send_kends(keys_to_send)  发送某个词到当前焦点的元素

    7、不常用的方法列表:

    (1)click_and_hold(on_element=None)  点击鼠标左键不松开

    (2)release(on_element=None)  在某个元素位置松开鼠标左键

    (3)key_down(value,element=None)  按下键盘上的某个键

    (4)key_up(value,element=None)  松开键盘上的某个键

    (5)drag_and_drop(source,target)  拖拽到某个元素然后松开

    (6)drag_and_drop_by_offset(source,xoffset,yoffset)  拖拽到某个坐标然后松开

    (7)move_by_offset(xoffset,yoffset)  鼠标从当前位置移动到某个坐标

    (8)move_to_element_with_offset(to_element,xoffset,yoffset)  移动到距某个元素(左上角坐标)多少距离的位置  

    例子:

    # coding utf-8
    from selenium import webdriver
    # 导入sleep这个模块
    from time import sleep
    from selenium.webdriver.common.action_chains import ActionChains

    # 打开淘宝首页
    url = "https://www.taobao.com/"
    driver = webdriver.Firefox()
    driver.get(url)
    # 睡眠时间3秒,看的效果明显点
    sleep(3)

    # 通过find_element_by_css_selector定位到“女装”
    menu_element = driver.find_element_by_css_selector("li.J_Cat:nth-child(1) > a:nth-child(1)")
    # 把鼠标移到女装上
    ActionChains(driver).move_to_element(menu_element).perform()

    sleep(10)
    #sreach_window=driver.current_window_handle
    # 选择女装中的“羽绒棉服”
    driver.find_element_by_xpath("/html/body/div[4]/div[1]/div[1]/div[1]/div/div/div[1]/div[1]/div[1]/p/a[3]").click()


    ---------------------------------------------------------------------------------

    关注微信公众号即可在手机上查阅,并可接收更多测试分享~

  • 相关阅读:
    Pikachu漏洞练习平台实验——SQL注入(四)
    ELMO,BERT和GPT简介
    Self-Attention 和 Transformer
    Seq2Seq和Attention机制入门介绍
    循环神经网络(RNN)入门介绍
    XGBoost的推导和说明
    在Azure DevOps Server中运行基于Spring Boot和Consul的微服务项目单元测试
    Azure DevOps Server(TFS) 客户端分析
    传统码头建设企业:Azure DevOps Server 流水线技术沟通
    ItelliJ Idea 2019提交TFVC变更,系统提示Validation must be performed before checking in
  • 原文地址:https://www.cnblogs.com/songzhenhua/p/14033463.html
Copyright © 2011-2022 走看看