简体   繁体   中英

Difficulty in sendline method of pexpect module

I need to automate a interview process by giving a predefined answers if I expect some string in the question of the interview.

I am using pexpect.spawn to start a child process and to go through the interview.

A portion of my code is :

child.expect("\> ")

child.sendline("test")

I am saving the interview process in a file using child.logfile=fout

A portion of fout is:

What would you like to use for the display name:^M
> ^M

Invalid input: ''^M

^M

What would you like to use for the display name:^M
> test


test^M

As seen above, it is matching a new line first time, hence the comment "Invalid input:". SO it asks the same question twice and then it accepts "test" as the answer.

What can I do to make my answer accepted for the very first time ?

Is there anything related to the parameter "timeout" in child.expect ?

I have tried using options such as timeout=0, timeout=-1 etc. But nothing is working.

wow get the answer: child.setecho(False)

worked on this issue for couple hours. luckily this works.

I think the issue lies in that whenever we send a line, if the line we sent matches the pattern we epxect (like the PROMPT we generally use. ) then before the result came out pexpect returns.

have to say we have to choose the EXPECTING PATTERN CAREFULLY. generally for me, I followed the recommended way in the example, setting PS1=[PEXPECT]\\$ and then setecho off then send command to change the PS1 this procedure works for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM