1. 程式人生 > 其它 >模擬windows鍵盤、滑鼠等操作模組:pywin32

模擬windows鍵盤、滑鼠等操作模組:pywin32

技術標籤:Python爬蟲seleniumwindows操作seleniumwindowpython

以下操作可以模擬對滑鼠鍵盤的一系列順序操作。

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()