kaliiiiiiiiii/Selenium-Driverless

add pywin32 functionality such as send_keys click and mouse move

Closed this issue · 5 comments

With pywin32 you can find a window via browser_pid
also the advantage of this library is that it can perform actions even in an inactive window.

and doesn't take the main mouse
This implementation will mimic real user input, but will not interfere with other applications in any way, unlike pyautogui.

import win32gui
import win32process
import win32con
import win32api

import win32gui
import win32con
import win32api
from time import sleep

def click(hwnd, x, y):
    lParam = win32api.MAKELONG(x, y)
    win32api.SendMessage(hwnd, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam)
    sleep(0.05)
    win32api.SendMessage(hwnd, win32con.WM_LBUTTONUP, 0, lParam)


def send_key(hwnd, key):
    if key == "\n":
        win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
        win32gui.PostMessage(hwnd, win32con.WM_KEYUP, win32con.VK_RETURN, 0)
    else:
        win32gui.PostMessage(hwnd, win32con.WM_CHAR, ord(key), 0)


def find_window_by_pid(pid): # you can also check the window name
    def callback(hwnd, hwnds):
        _, found_pid = win32process.GetWindowThreadProcessId(hwnd)
        if found_pid == pid:
            hwnds.append(hwnd)
        return True

    hwnds = []
    win32gui.EnumWindows(callback, hwnds)
    return hwnds[0] if hwnds else None

ah yep I'm aware of this one.
Working with some other guy on that. currently as well.
I consider that a part of #94
Therefore closing as dublicate

The hardest part is figuring out how to interact with elements without driver.find
I have three ideas so far

  1. open the page, record coordinates and reload it
  2. use driver.page_source to find elements and use their coordinates
  3. search by images

Wow, I haven't seen anything so ordinary in years.