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 "

  • 相关阅读:
    oracle 自动备份,删除历史
    oracle 系统语法
    myeclipse快捷键
    ANT教程
    新手 Spring 疑惑
    MyEclipse8.5整合Git (转)
    eclipse build path功能总结
    Apache Maven 入门篇(下)
    Mac OS下配置PHP Nginx PHP-FPM
    Python删除列表中元素
  • 原文地址:https://www.cnblogs.com/geminichao/p/5672546.html
Copyright © 2011-2022 走看看