【问题标题】:pexpect send cursor movementpexpect 发送光标移动
【发布时间】:2021-11-09 10:38:38
【问题描述】:

如何使用 pexpect 发送光标移动,如上、下、左、右键。下面的示例是自动化 elinks,它使用 up/down 键来选择页面上的不同链接。

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

【问题讨论】:

    标签: python pexpect


    【解决方案1】:

    下面的脚本包含所有四个光标移动的代码,以及如何在 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
    

    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)
    

    【讨论】:

      【解决方案2】:

      像这样对 up(^[[A) 或 down(^[[B) 使用转义序列怎么样。

      child.send("\033[A")  # up
      child.send("\033[B")  # down
      

      【讨论】:

        【解决方案3】:

        试试这个发送'\033\117\102'作为向下键

        【讨论】:

        • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-19
        • 1970-01-01
        • 1970-01-01
        • 2018-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多