zoukankan      html  css  js  c++  java
  • selenium.模拟鼠标操作(ActionChains)

    鼠标事件
    webdriver模块中的件方法:
    clear()        #清楚输入框的内容
    send_keys('内容')  #在文本框内输入内容
    click()        #点击按钮
    submit()        #表单的提交

    ActionChains模块中的方法:

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

    click_and_hold(on_element=None)                           #点击鼠标左键,按住不放

    context_click(on_element=None)                             #点击鼠标右键

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

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

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

    move_by_offset(xoffset, yoffset)                           #鼠标移动到距离当前位置(x,y)

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

    move_to_element_with_offset(to_element, xoffset, yoffset)  #将鼠标移动到距某个元素多少距离的位置

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

    perform()                                                  #执行链中的所有动作

    ActionChains的两种写法:

    
    

    #首先导入模块
    from selenium.webdriver.common.action_chains import ActionChains


    #链条式方法
    searchElement = driver.find_element_by_id('sb_form_q').send_keys('selenium')
    searchButtonElement = driver.find_element_by_id('sb_form_go')
    ActionChains(driver).click(searchButtonElement).perform()


    #分布式方法
    searchElement = driver.find_element_by_id('sb_form_q').send_keys('selenium')
    searchButtonElement = driver.find_element_by_id('sb_form_go')
    ActionChainsDriver = ActionChains(driver).click(searchButtonElement)
    ActionChainsDriver.perform()

     

    在12306主页做一个练习,效果如gif

    from selenium.webdriver.common.action_chains import ActionChains
    from selenium import webdriver
    from time import sleep
    
    get_12306 = webdriver.Firefox()
    get_12306.get('https://www.12306.cn/index/index.html')
    
    g_href = get_12306.find_element_by_xpath('//*[@id="J-index"]/a')
    Action = ActionChains(get_12306)
    
    
    for x in range(9):
        x = x * 145
        print(x)
        Action.move_to_element_with_offset(g_href, x, 0).perform()
        sleep(0.5)
    
    sleep(2)
    
    get_12306.quit()
  • 相关阅读:
    火狐插件火狐黑客插件将Firefox变成黑客工具的七个插件
    memcache安装环境:WINDOWS 7
    PHP正则表达式
    968. 监控二叉树 力扣(困难) dfs 官方说DP
    375. 猜数字大小 II 力扣(中等) 区间动态规划、记忆化搜索
    629. K个逆序对数组 力扣(困难) 区间动态规划
    剑指 Offer 51. 数组中的逆序对 力扣(困难) 巧用归并排序算法
    488. 祖玛游戏 力扣(困难) dfs
    16. 最接近的三数之和 力扣(中等) 双指针
    319. 灯泡开关 力扣(中等) 数论
  • 原文地址:https://www.cnblogs.com/youngleesin/p/10449356.html
Copyright © 2011-2022 走看看