Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
924 views
in Technique[技术] by (71.8m points)

Input unicode string with pyautogui

I'm creating an autotesting app with pyautogui lib. I want to use typewrite method to input text into forms. But some of my input strings have unicode characters in them. For example:

N?st

According to documentation typewrite can only press single-character keys. So it just ignores the ? character.

Can you advise some simple workaround?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I know this thread is old, but for the sake of the topic I managed to get around it using pyperclip in an easier manner in my opinion.

Rather than trying to make pyautogui to type special characters, copy them to the clipboard using pyperclip and then use pyautogui to paste them. For instance on Windows:

import pyautogui
import pyperclip

pyperclip.copy("It's leviOsa, not lêvio?á!")
pyautogui.hotkey("ctrl", "v")

EDIT:

We can make it work in multiple platforms as below (thanks @karlo for pointing it out):

import pyautogui
import pyperclip
import platform

def type(text: str):    
    pyperclip.copy(text)
    if platform.system() == "Darwin":
        pyautogui.hotkey("command", "v")
    else:
        pyautogui.hotkey("ctrl", "v")


type("It's leviOsa, not lêvio?á!")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...