pyautogui.write() 不会自动使用shift 在非英语键盘上输入字符

问题描述 投票:0回答:1

我正在尝试自动将文件上传到网站。使用

selenium
导航网站,我得到了输入文件名的提示框:

Upload prompt box

要输入文件名,我想到使用

pyautogui
(但如果有更好的方法,我会采用它,因为目前,我正在使用 headful 导航器进行测试,但我不确定使用
当我尝试使用无头导航器时,pyautogui
将会工作。),就像这样:

import pyautogui 
path = r"C:\path\to_my_file.pdf"
pyautogui.write(path)
pyautogui.hotkey('enter')

我的问题是,pyautogui 不是键入

path
的内容,而是键入与每个字符对应的键盘按键。因此,我得到
C/\path\to8my8file.pdf
,因为我有一个法语键盘(其中数字为大写,特殊字符如
_
:
为小写)。

当然,我可以确保当 pyautogui 输入

:
_
时,它会执行需要做的事情
shift
+
/
shift
+
8
,但这将是艰苦的,而且它必须针对每个特定用例进行。我确信有一种更好的通用方法来精确输入
path
的内容。

旁注:

一个老问题的答案是

pyautogui.write('C:\path\to_my_file.pdf', interval=0.1)
pyautogui.typewrite("C:\path\to_my_file.pdf", interval=0.1)
。它们不起作用。

python pyautogui
1个回答
0
投票

如果您想要输入文字,请使用
pynput
。它是众多软件包中对打字支持最高的。

安装:

pip install pynput

示例:

from pynput.keyboard import Controller
import time
keyboard = Controller()

txt = r'''1. Overview of Owls
Owls belong to the order Strigiformes, which comprises over 200 species divided into two main families: Tytonidae (barn owls) and Strigidae (true owls). They are found on every continent except Antarctica, thriving in diverse habitats, from dense forests to open grasslands and urban areas.

Physical Characteristics
Owls are easily recognizable due to their distinctive features:

Large Eyes: Adapted for low light, owl eyes are forward-facing, providing excellent binocular vision.
Silent Flight: Their specialized wing structure allows them to fly silently, aiding in stealthy hunting.
Facial Disc: The rounded facial disc helps funnel sound to their ears, enhancing their ability to locate prey.
Size and Coloration
Owls vary significantly in size, from the tiny elf owl, measuring about 5-6 inches, to the impressive Eurasian eagle owl, which can have a wingspan of nearly 6 feet. Their plumage is often mottled or camouflaged, allowing them to blend into their surroundings.

        .-"""""""-.    .'""."".""`.
       /   \ * /   \  /  ^ \   /^  \
      { ((@)\*/((@) }.{ ~Q '\ / Q ~}}
     ( `.~~ .V. ~~.'}.`._.'  V`._.'  )
     ( ( `-')  `-'  }   ~~     ~~    )
     (  (   ) ^ ^   0 "  " "   (  (   )
     (      )  ^^  00   "      (      .)
     ( (   )     ^ 00     "    ( (   ) )
      {   )  ^^   .0'0  "   "   (     }
       { }  ^   ))    0          (   }
        `--叩w叩--'       "--叩m叩------ "

'''

time.sleep(5) # 5 seconds for you to ready!

# start typing
for s in txt:
    keyboard.type(s)
    time.sleep(0.0015) # delay for visual enjoyment

time.sleep(3) # maintain the main thread

运行此命令以获取文本区域:

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Simple Text Editor</title><style>body{font-family:Arial,sans-serif;display:flex;justify-content:center;align-items:center;height:100vh;margin:0;background-color:#000;}textarea{width:80%;height:80%;font-family:'Courier New',monospace;font-size:16px;padding:10px;border:1px solid #ccc;border-radius:5px;resize:none;box-shadow:0 2px 5px rgba(255,255,255,0.1);background-color:#222;color:#fff;}@media (max-width:600px){textarea{width:95%;height:70%;}}</style></head><body><textarea placeholder="Type your text here..."></textarea></body></html>

© www.soinside.com 2019 - 2024. All rights reserved.