pexpect发送光标移动

Mar*_*agh 3 python pexpect

如何通过pexpect发送光标移动,如上,下,左,右键。下面的示例是自动化链接,该链接使用向上/向下键在页面上选择不同的链接。

from pexpect import spawn
child = spawn('elinks http://python.org')
#what goes here to send down key
child.interact()
Run Code Online (Sandbox Code Playgroud)

Mar*_*agh 5

下面的脚本包含了所有四个光标移动的代码,并举例说明了如何在pexpect中使用它。要发现任何键入的文本的确切字符串序列,可以使用下面的get_keys.py脚本。

KEY_UP = '\x1b[A'
KEY_DOWN = '\x1b[B'
KEY_RIGHT = '\x1b[C'
KEY_LEFT = '\x1b[D'
child.sendline(KEY_DOWN * 5)  #send five key downs
Run Code Online (Sandbox Code Playgroud)

get_keys.py

import curses
screen = curses.initscr()

screen.addstr("Press any set of keys then press enter\n") 
keys = ''
while True:
   event = screen.getkey()
   if event == "\n":
       break
   keys += event

curses.endwin()
print repr(keys)
Run Code Online (Sandbox Code Playgroud)