I'm trying to automate the uploading of a file to a website. Using selenium
to navigate the website, I got to the point where I get the prompt box to input the filename:
To enter the filename, I thought of using pyautogui
(but if there is a better way, I'll take it, because at the moment, I'm testing with a headful navigator, but I'm not sure using pyautogui
will work when I try to use a headless navigator.), like such:
import pyautogui
path = r"C:\path\to_my_file.pdf"
pyautogui.write(path)
pyautogui.hotkey('enter')
My problem is that instead of typing the content of path
, pyautogui types the keyboard keys that correspond to each character. Therefore, I get C/\path\to8my8file.pdf
, because I have a french keyboard (where numbers are uppercase and special characters like _
and :
are lowercase).
Of course, I could make sure that when pyautogui types :
and _
, it does what's needed to do shift
+ /
and shift
+ 8
, but that would be painstaking, and it would have to be done to each specific usecase. I'm sure there is a better, generic way, to type precisely the content of path
.
Sidenote:
An old SO question had the answers pyautogui.write('C:\path\to_my_file.pdf', interval=0.1)
or pyautogui.typewrite("C:\path\to_my_file.pdf", interval=0.1)
. They do not work.
pynput
, if typing words automatically is what you want to do. It has the best support in auto-typing among many packages.capital letters
✓ non-english characters
✓
Install:
pip install pynput
Example:
from pynput.keyboard import Controller
import time
keyboard = Controller()
txt = r'''Non-English: 哈あµø∫
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.
.-"""""""-. .'""."".""`.
/ \ * / \ / ^ \ /^ \
{ ((@)\*/((@) }.{ ~Q '\ / Q ~}}
( `.~~ .V. ~~.'}.`._.' V`._.' )
( ( `-') `-' } ~~ ~~ )
( ( ) ^ ^ 0 " " " ( ( )
( ) ^^ 00 " ( .)
( ( ) ^ 00 " ( ( ) )
{ ) ^^ .0'0 " " ( }
{ } ^ )) 0 ( }
`--叩w叩--' "--叩m叩------ "
'''
print("Focus on the text area in 6 seconds!")
time.sleep(6) # 6 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) # <-- rmb to maintain the main thread in case you run keyboard.type(my_str_paragraph)
Text Area:
<!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:90%;height:85%;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>