您的位置:首页 > 编程语言 > Python开发

Python Selenium 键盘和鼠标操作

2015-11-10 14:02 781 查看
原文章地址:

http://blog.sina.com.cn/s/blog_620987bf0102v2r8.html

      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 \n"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: