模拟windows键盘、鼠标等操作模块:pywin32

使用到的模块:

from selenium import webdriver

from selenium.webdriver.common.action_chains import ActionChains
import time
import os
import win32con
import win32api
import win32clipboard

以下操作可以模拟对鼠标键盘的一系列顺序操作。

1、 将内容复制到剪切板:

# 将字符串text复制到剪切板
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardText('text')
win32clipboard.CloseClipboard()

2、 鼠标定位当前页面位置

win32api.SetCursorPos([200,370])  #数值[水平位置,垂直位置]

3、 鼠标单击和右击

#执行左单键击,若需要双击则延时几毫秒再点击一次即可
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP | win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
#右键单击
win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP | win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)

4、 键盘点击操作

# Ctrl+s (Ctrl+a、Ctrl+v操作类似)
win32api.keybd_event(17, 0, 0, 0) # 按下Ctrl键
win32api.keybd_event(83, 0, 0, 0) # 按下s键
win32api.keybd_event(83, 0, win32con.KEYEVENTF_KEYUP, 0) # 释放Ctrl键
win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)
# 按下回车并释放回车键
win32api.keybd_event(13, 0, 0, 0)
win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)

【下面为键盘键值表:】
pywin32模拟键盘操作时的键盘键值表

5、使用示例

基于pywin32,模仿鼠标键盘操作,可以实现完整网页的另存为操作(包括html以及图片链接等,效果与手动保存网页效果相同),示例如下:

def saveWeb_main2(url="https://www.baidu.com"):
    driver = webdriver.Firefox()
    driver.get(url)
    driver.maximize_window()
    time.sleep(2)
    title = driver.title
    # 设置路径为:当前项目的绝对路径+文件名
    # path = (os.path.dirname(os.path.realpath(__file__)) + "\\" + title + ".html")
    # 定位到指定网页元素,鼠标右击操作,context_click()
    # context = driver.find_element_by_css_selector(".index-logo-src")
    # ActionChains(driver).context_click(context).perform()
    # time.sleep(3)
    
    # 将路径复制到剪切板
    win32clipboard.OpenClipboard()
    win32clipboard.EmptyClipboard()
    win32clipboard.SetClipboardText(title)
    win32clipboard.CloseClipboard()

    # #1、鼠标定位到(30,50)
    # win32api.SetCursorPos([200,150])
    # #执行左单键击,若需要双击则延时几毫秒再点击一次即可
    # # win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP | win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
    # #右键单击
    # win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP | win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)

    # #2、按下下键,选择“网页另存为”
    # win32api.keybd_event(40, 0, 0, 0)
    # time.sleep(1)
    # win32api.keybd_event(40, 0, 0, 0)
    # time.sleep(1)
    # win32api.keybd_event(40, 0, 0, 0)
    # time.sleep(1)
    # win32api.keybd_event(40, 0, 0, 0)
    # time.sleep(1)
    # win32api.keybd_event(40, 0, 0, 0)
    # time.sleep(1)
    # win32api.keybd_event(40, 0, 0, 0)
    # time.sleep(1)

    # #3、 按下回车
    # win32api.keybd_event(13, 0, 0, 0)
    # time.sleep(1)
    # #4、 释放回车键
    # win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)
    # time.sleep(1)
    # #5、 释放下键
    # win32api.keybd_event(40, 0, win32con.KEYEVENTF_KEYUP, 0)
    # time.sleep(1)

    # Ctrl+s 可以替换上述1-5步
    win32api.keybd_event(17, 0, 0, 0)
    win32api.keybd_event(83, 0, 0, 0)
    win32api.keybd_event(83, 0, win32con.KEYEVENTF_KEYUP, 0)
    win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)
    time.sleep(1)

    #鼠标定位输入框并点击
    win32api.SetCursorPos([200,370])  # 输入框的定位可能不同的尺寸会有不同定位位置,可以自己多次尝试
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP | win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
    # win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP | win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
    time.sleep(1)

    # 按下ctrl+a
    win32api.keybd_event(17, 0, 0, 0)
    win32api.keybd_event(65, 0, 0, 0)
    win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)
    win32api.keybd_event(65, 0, win32con.KEYEVENTF_KEYUP, 0)
    time.sleep(1)

    # 按下ctrl+v
    win32api.keybd_event(17, 0, 0, 0)
    win32api.keybd_event(86, 0, 0, 0)
    win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)
    win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)
    time.sleep(1)

    # 按下回车
    win32api.keybd_event(13, 0, 0, 0)
    win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)
    time.sleep(10) #需要设置时间长一些,否则时间过短,页面下载不完整

    driver.quit()

常见异常问题

1、问题:ImportError: DLL load failed while importing win32api: 找不到指定的程序。
 解决:https://github.com/michaelgundlach/pyspeech/issues/23
I have the same problem for days, but one day I solved it.
After you installed the pywin32 libs, there is a directory “Lib/site-packages/pywin32_system32”, which including three dll libs, copy them to the “/Lib/site-packages/win32” directory, which including the win32api.pyd or win32api.pyc.
There will be no ImportError Exception any more

  • 10
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值