-1

I have a while loop that navigates to a webpage that I manually inspect. What I need to be able to do is press the enter key and have the counter stored. Ideally everything would work like this:

from selenium import webdriver as wd
import time

url = [www.example.com,www.google.com]
ff = wd.Firefox()
stored_url_number = []

i=0
while i<len(url):
    ff.get(url[i])
    time.sleep(5)
    if enterispressed:
         stored_url_number.extend(i)
    i +=1

Is there a simple way to create an 'enterispressed' function in python?

Specifically, I want the program to run as normal to the next iteration unless enter is pressed, in which case I want it to perform the .extend(i) action and then move on.

Many thanks

draco_alpine
  • 769
  • 11
  • 25

1 Answers1

1

Simple way is to get input from the user and check if it's empty:

while i<len(url):
    ff.get(url[i])
    time.sleep(5)
    x = input()
    if x=="":
         stored_url_number.extend(i)
    i +=1

You can skip the checking if you don't care what the input is:

while i<len(url):
    ff.get(url[i])
    time.sleep(5)
    input()
    stored_url_number.extend(i)
    i +=1

Edit:

That works for python 3.

For python 2 use raw_input() instead of input().

Edit 2:

If you want to limit the time for the press you should look here or here

Community
  • 1
  • 1
Ohad Eytan
  • 8,114
  • 1
  • 22
  • 31
  • I've run some test cases with this and it seems that this has a few issues. I've edited the question to reflect some of these, but the return key is not a a legitimate input for input() and giving an EOF error. – draco_alpine Jul 14 '16 at 11:20
  • @draco_alpine I just checked it and it's works on python3 but not on python2.7 :( – Ohad Eytan Jul 14 '16 at 11:24