selenium+pythonAutomatic 99– file download pop-up processing (PyKeyboard)
Preface
In the web automation download operation, sometimes pop-up download box, this kind of download box does not belong to the web page, is unable to locate (some students say click, the head is positioning! Location! Position!)
Sometimes we don’t have to locate the button and click on it, and learn to use the keyboard’s shortcuts to achieve the same effect.
A previous article on Selenium 2 + Python Automation 75 – Non-Input File Uploads (SendKeys) was written based on Python 2.
Recently, a lot of kids have started using Python 3, and this SendKeys isn’t working on Python 3, which requires PyUserInput to install the tutorial address Selenium + Python Automation 84-python 3.6 uses PyUserInput
Install dependency packets:
- Rely on pywin32
- Rely on pyHook
PyKeyboardKeyboard operation
PyUserInputThere are two main modules in the module.
- PyMouse, Specialized simulation of mouse operation
- PyKeyboard,Special simulation of keyboard operation
First use manual to operate on the keyboard, remember the operation steps: press the Tab key – press the Enter key.
Code reference
# coding:utf-8
from selenium import webdriver
from pykeyboard import PyKeyboard
from pymouse import PyMouse
import time
driver = webdriver.Firefox()
driver.get("https://www.autoitscript.com/files/autoit3/autoit-v3-setup.exe")
time.sleep(3)
# By default, on the Cancel button, switch to the save file first.K = PyKeyboard ()Send tabK.press_key (k.tab_key)K.release_key (k.tab_key)Time.sLEEP (3)Please send back.
Two methods are used here: press_key holding down the Tab key and release_key releasing the key. There is another way, tap_key.
tap_keyAnalog clicks
Look at how the tap_key source is designed, and tap_key is actually the encapsulated press_key and release_key methods
- character Passing corresponding keyboard events
- n=1 Only one click by default.
- interval=0 If there are multiple clicks, the default time of sleep is 0.
def tap_key(self, character='', n=1, interval=0):
"""Press and release a given character key n times."""
for i in range(n):
self.press_key(character)
self.release_key(character)
time.sleep(interval)
Change to tap_key operation
# coding:utf-8
from selenium import webdriver
from pykeyboard import PyKeyboard
from pymouse import PyMouse
import time
# **Author: Shanghai Yo Yo, QQ exchange group: 646645429**Driver = webdriver.Firefox ()Driver.get ("https://www.autoitscript.com/f")Iles/autoit3/autoit-v3-setup.exe ""Time.sleep (3)The default is to switch to the save file on the Cancel button.K = PyKeyboard ()Analog TabK.TAp_key (k.tab_key)Time.sleep (3)Enter send backK.tap_key (k.enter_key)
PyKeyboardOther operations
Besides simulating tab and enter, it can also simulate input contents in input box.
Basic operation methods, such as input h:k.tap_key (“H”).
# coding:utf-8
from selenium import webdriver
from pykeyboard import PyKeyboard
from pymouse import PyMouse
import time
# **Author: Shanghai Yo Yo, QQ exchange group: 646645429**Driver = webdriver.Firefox ()Driver.get ("https://www.baidu.com/")Time.Sleep (3)K = PyKeyboard ()Def input_str (s):Enter a string of English ''For I in s:K.tap_key (I)K.tab_key (k.enter_key)Input_str ("HelloWorld!")