Selenium+python automated 98--File download popup processing (pykeyboard)

Source: Internet
Author: User

Objective

In the Web automation download operation, sometimes will pop up the download box, this download box does not belong to the Web page, there is no way to locate the (some students say click, the head is positioning!) Positioning! Positioning! )
Sometimes we do not have to go to this button to click, learn to use the keyboard shortcut key operation, but also to achieve the same effect.
I've said a Selenium2+python Automation 75-Non-input file upload (SendKeys) This was written based on Python2.
Recently a lot of small partners began to use Python3, this SendKeys on the Python3 can not be used, python3 need to use Pyuserinput, detailed installation of the tutorial address "selenium+ SendKeys error on Python automation 85-python3.6 replaced with Pyuserinput "

To install a dependency package:

    • Dependent Pywin32
    • Dependent Pyhook
Pykeyboard keyboard operation

There are two main types of pyuserinput modules:

    • Pymouse, specifically simulates mouse operation
    • Pykeyboard, specifically simulates the operation on the keyboard

First manually on the keyboard, remember the procedure: Press the TAB key--Press the ENTER key

Code reference

# coding:utf-8from selenium import webdriverfrom pykeyboard import PyKeyboardfrom pymouse import PyMouseimport timedriver = webdriver.Firefox()driver.get("https://www.autoitscript.com/files/autoit3/autoit-v3-setup.exe")time.sleep(3)# 默认在取消按钮上,先切换到保存文件上k = PyKeyboard()# 发送tabk.press_key(k.tab_key)k.release_key(k.tab_key)time.sleep(3)# 发送回车

Here are two methods, one is Press_key hold the TAB key, the other is the Release_key release button. There's actually another way Tap_key

Tap_key Analog Click

First look at the Tap_key source code is how to design, in fact Tap_key is the package of Press_key and Release_key these 2 methods

    • Character the corresponding keyboard event
    • N=1 by default only once.
    • Interval=0 If there are multiple clicks, the middle gap time, the default sleep time 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-8from selenium import webdriverfrom pykeyboard import PyKeyboardfrom pymouse import PyMouseimport time#  **上海-悠悠, QQ交流群:646645429**driver = webdriver.Firefox()driver.get("https://www.autoitscript.com/files/autoit3/autoit-v3-setup.exe")time.sleep(3)# 默认在取消按钮上,先切换到保存文件上k = PyKeyboard()# 模拟Tabk.tap_key(k.tab_key)time.sleep(3)# 发送Enter回车k.tap_key(k.enter_key)
Pykeyboard Other operations

In addition to simulating the operation of Tab,enter, you can also simulate input in the input box.
Basic operating methods, such as input h:k.tap_key ("H")

# coding:utf-8from selenium import webdriverfrom pykeyboard import PyKeyboardfrom pymouse import PyMouseimport time#  **上海-悠悠, QQ交流群:646645429**driver = webdriver.Firefox()driver.get("https://www.baidu.com/")time.sleep(3)k = PyKeyboard()def input_str(s):    '''输入一串英文'''    for i in s:        k.tap_key(i)    k.tab_key(k.enter_key)input_str("helloworld!")

Shanghai-leisurely, QQ Exchange Group: 646645429

Selenium+python automated 98--File download popup processing (Pykeyboard)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.