I believe that when you use selenium to automate web pages, you will encounter the following scenario:
When you use get to access a certain URL, a pop-up box as shown in the figure above will pop up on the page.
The first thought is to use the Alert class to handle it.
However, unfortunately, the result of the Alert class processing is no result, and the pop-up box cannot be closed.
The pop-up box cannot be closed by processing the alert, and other operations on the page cannot be performed without closing it. What should I do?
solution
Use the keyboard's Enter key to close the popup.
Since the send_keys method can only send the key value of selenium for the element, the current case cannot locate the pop-up box.
So use PyKeyboad to implement keyboard operation with python
Specific solution steps
Step 1: To use PyKeyboard, you need to install: pywin32->pyHook->PyUserInput
(1) Install pywin32
When installing pywin32 from foreign sources, you will often encounter installation failures due to timeouts, so it is recommended to install from domestic sources.
You can install pywin32 from domestic sources through cmd, the command is as follows:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pywin32
Among them: https://pypi.tuna.tsinghua.edu.cn/simple is the mirror file address of Tsinghua University.
Other domestic sources are also available, such as:
Alibaba Cloud: https://mirrors.aliyun.com/pypi/simple/
Douban: https://pypi.douban.com/simple/
(2) How to install pyHook
After downloading the corresponding version of pyHook from https://blog.csdn.net/dianmomanxue/article/details/95044676, enter the folder path of the pyHook through cmd, and enter:
pip install pyHook1.5.1cp37cp37mwin_amd64.whl
(3) How to install PyUserInput
Through cmd, enter: pip install PyUserInput
Step 2: Import PyKeyboard in the py file and enter the Enter key:
If you need to press other keys, select PyKeyboard and press ctrl+b to view the description of the properties under the special_key_assignment function.
The complete processing code of the screenshot at the beginning of this article is as follows:
Finally: In order to give back to the die-hard fans, I have compiled a complete software testing video learning tutorial for you. If you need it, you can get it for free【保证100%免费】
Software Testing Interview Documentation
We must study to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Ali, Tencent, and Byte, and some Byte bosses have given authoritative answers. Finish this set The interview materials believe that everyone can find a satisfactory job.