zoukankan      html  css  js  c++  java
  • selenium+Python(三)键盘和鼠标操作

    Python也可以模拟鼠标和键盘的操作,不过要注意的是键盘带来的屏幕游标位置的挪动和鼠标在屏幕上的挪动位置,两个是不同的。

          首先要在文件头引入

          from selenium.webdriver.common.action_chains import ActionChains

    #定义一个函数
    def Transfer_Clicks(browser):
        browser.execute_script("window.scrollBy(0,-document.body.scrollHeight)","") 

       #这个是执行一段Javascript函数,将网页滚到到网页顶部。
            try:
                inputs1 = browser.find_elements_by_class_name("feedAttr_transfer")
                for input1 in inputs1:
                    try:
                        ActionChains(browser).click(input1).perform()

                       #模拟鼠标点击控件input1,此时的鼠标位置在input1处
                        browser.execute_script("window.scrollBy(0,200)","")
                        #向下滚动200个像素,鼠标位置也跟着变了
                        ActionChains(browser).move_by_offset(0,-80).perform()

                       #向上移动鼠标80个像素,水平方向不同
                        ActionChains(browser).click().perform()

                       #鼠标左键点击
                        ActionChains(browser).key_down(Keys.TAB).perform()
                        #模拟tab键的输入

                        ActionChains(browser).send_keys(Keys.ENTER).perform()
                        #模拟输入ENTER键
                    except:
                        pass
            except:
                pass
            return "Transfer successfully "

  • 相关阅读:
    61. Rotate List
    60. Permutation Sequence
    59. Spiral Matrix II
    57. Insert Interval
    18多校8th
    2019山东省赛总结
    二分图——poj2239
    二分图匹配——poj1469
    二分图——poj2446匈牙利算法
    思维构造,建图——cf1159E
  • 原文地址:https://www.cnblogs.com/geminichao/p/5672546.html
Copyright © 2011-2022 走看看