前言
press方法是用来模拟键盘按键输入,可以组合使用,来模拟键盘输入,解决一些难定位的元素
入参介绍
以下是press源码中的函数介绍
def press(key): | |
:入参 | :param key: Key or combination of keys to be pressed. |
Presses the given key or key combination. To press a normal letter key such | |
as 'a' simply call `press` for it:: | |
举例 1 | press('a') |
You can also simulate the pressing of upper case characters that way:: | |
举例2 | press('A') |
The special keys you can press are those given by Selenium's class | |
:py:class:`selenium.webdriver.common.keys.Keys`. Helium makes all those keys | |
available through its namespace, so you can just use them without having to | |
refer to :py:class:`selenium.webdriver.common.keys.Keys`. For instance, to | |
press the Enter key:: | |
举例3 | press(ENTER) |
To press multiple keys at the same time, concatenate them with `+`. For | |
example, to press Control + a, call:: | |
举例4 | press(CONTROL + 'a') |
""" | |
_get_api_impl().press_impl(key) |
以下是所有的入参关键字:
NULL = Keys.NULL |
CANCEL = Keys.CANCEL |
HELP = Keys.HELP |
BACK_SPACE = Keys.BACK_SPACE |
TAB = Keys.TAB |
CLEAR = Keys.CLEAR |
RETURN = Keys.RETURN |
ENTER = Keys.ENTER |
SHIFT = Keys.SHIFT |
LEFT_SHIFT = Keys.LEFT_SHIFT |
CONTROL = Keys.CONTROL |
LEFT_CONTROL = Keys.LEFT_CONTROL |
ALT = Keys.ALT |
LEFT_ALT = Keys.LEFT_ALT |
PAUSE = Keys.PAUSE |
ESCAPE = Keys.ESCAPE |
SPACE = Keys.SPACE |
PAGE_UP = Keys.PAGE_UP |
PAGE_DOWN = Keys.PAGE_DOWN |
END = Keys.END |
HOME = Keys.HOME |
LEFT = Keys.LEFT |
ARROW_LEFT = Keys.ARROW_LEFT |
UP = Keys.UP |
ARROW_UP = Keys.ARROW_UP |
RIGHT = Keys.RIGHT |
ARROW_RIGHT = Keys.ARROW_RIGHT |
DOWN = Keys.DOWN |
ARROW_DOWN = Keys.ARROW_DOWN |
INSERT = Keys.INSERT |
DELETE = Keys.DELETE |
SEMICOLON = Keys.SEMICOLON |
EQUALS = Keys.EQUALS |
NUMPAD0 = Keys.NUMPAD0 |
NUMPAD1 = Keys.NUMPAD1 |
NUMPAD2 = Keys.NUMPAD2 |
NUMPAD3 = Keys.NUMPAD3 |
NUMPAD4 = Keys.NUMPAD4 |
NUMPAD5 = Keys.NUMPAD5 |
NUMPAD6 = Keys.NUMPAD6 |
NUMPAD7 = Keys.NUMPAD7 |
NUMPAD8 = Keys.NUMPAD8 |
NUMPAD9 = Keys.NUMPAD9 |
MULTIPLY = Keys.MULTIPLY |
ADD = Keys.ADD |
SEPARATOR = Keys.SEPARATOR |
SUBTRACT = Keys.SUBTRACT |
DECIMAL = Keys.DECIMAL |
DIVIDE = Keys.DIVIDE |
F1 = Keys.F1 |
F2 = Keys.F2 |
F3 = Keys.F3 |
F4 = Keys.F4 |
F5 = Keys.F5 |
F6 = Keys.F6 |
F7 = Keys.F7 |
F8 = Keys.F8 |
F9 = Keys.F9 |
F10 = Keys.F10 |
F11 = Keys.F11 |
F12 = Keys.F12 |
META = Keys.META |
COMMAND = Keys.COMMAND |
press案例分析
1、我们来介绍一下滚动页面的应用
代码:
start_chrome("https://www.icourse163.org/")
time.sleep(2)
wait_until(Text("学校").exists)
highlight(Text("学校"))
time.sleep(2)
click(Text("学校"))
press(END)
time.sleep(10)
kill_browser()
效果:
看到右侧滚动条已经滚动到最下方
2、利用TAB键切换位置
如下图,先定位“1”,press(TAB),跳转到“贴吧搜索”,press(TAB),跳转到“2”,press(TAB),跳转到“3”
欢迎关注
weitungblog