可以从pexpect.expect获取匹配结果吗?

2 投票
1 回答
2256 浏览
提问于 2025-04-18 14:38

我正在自动化测试一个动态生成的菜单,也就是说,每个菜单项前面可能会有几个不同的数字。我可以这样写:

a = child.expect('1\) Set Password', '2\) Set Password', '3\) Set Password')
if a == 0: child.sendline('1')
elif a == 1: child.sendline('2')
elif a == 2: child.sendline('3')

但是这样写起来有点麻烦。我更希望能有类似这样的写法:

child.expect('(\d)\) Set Password')
a = child.get_match()
child.sendline(a)

有没有这样的函数呢?

1 个回答

3

Thomas K 在评论中给出了这个答案:

 child.match.group(1)

这应该能帮你得到你想要的那一位。

而且,确实是这样。

撰写回答