0

I'm writing a simple script to login a specific site, enter my credentials, and navigate to specific section. I'm using selenium to control Firefox and the first portion is working fine.

The next part is where I'm having issues.

Basically, after logging in and going Section1, I created a while loop that asks for an input from me. The inputs would be to navigate between different sections. I have 5 options here. The 5th one logs out of the site and quits the program.

I'm running this on Mac (terminal). The behavior I want is I only choose one of the options (1, 2, 3, 4, 5) to navigate between the different sections and/or logout and close the program.

Here's the code that code that comes after I open up Firefox, login, and go to section 1.

reply = ''
while reply == '':
    print('''
    What would you like to do next?
    Enter the number to do the following:
    1 - Go to Section 1
    2 - Go to Section 2
    3 - Go to Section 3
    4 - Go to Section 4
    5 - Logout and Quit
    ''')
    
    reply = input()

    if reply == 1: #Load Section 1
        section1 = browser.find_element_by_id('id goes here')
        section1.click()
    

    elif reply == 2:  #Load Section 2
        print('Opening negotations...')
        section2 = browser.find_element_by_id('id goes here')
        section2()  
        
    elif reply == 3: #Load Section 3
        section3 = browser.find_element_by_id('id goes here')
        section3.click()
            
    elif reply == 4: #Load Section 4
        section4 = browser.find_element_by_id('id goes here')
        section4.click()
                
    elif reply == 5: #Logout and close browser
        break 
    
    else:
        print('Please choose from one of the options.')
    
print('Logging out...')
logout = browser.find_element_by_css_selector('css selector goes here')
logout.click()
time.sleep(3)
browser.quit()

All the individual find_element_by_css_selector or by_id_selector are working individually. I just can't get it to work inside the loop.

What's happening is that every time I get asked for the input (reply), no matter what my answer is, it logs out, i.e. performs the 5th option / out of the while loop.

I can't seem to understand what's causing it.

Some stuff I tried

Adding a continue after each if/elif like this:

if reply == 1: #Load Section 1
        section1 = browser.find_element_by_id('id goes here')
        section1.click()
        continue

But still goes to the end of my code.

Community
  • 1
  • 1
aylim14
  • 49
  • 1
  • 8
  • 3
    It does that because that's what you've asked it to do: your loop continues *while* reply is an empty string, but the first thing you do is to set reply to a non-empty string. – Daniel Roseman Sep 04 '18 at 10:30
  • `reply == ''` is no longer true the second you read the first input. Can't expect another iteration then. – user2390182 Sep 04 '18 at 10:31

3 Answers3

2

You need to set reply back to empty string in your else.

else:
    print('Please choose from one of the options.')
    reply = ''

When you select an unavailable option, you're setting reply, then your loop is checking to see if reply is equal to empty string (''). By setting it back to empty string, the loop can continue.

The other answer here also make a good point about casting the input properly. The input is coming back as a string and that string is being assigned to reply. Immediately afterwards, you're checking to see if the string that is reply is equal to an integer. This is a good fix to address, but it is not the source of your loop issue, resetting the reply variable back to empty string is.

Joshua Schlichting
  • 3,110
  • 6
  • 28
  • 54
  • Thanks! Combining everyone's reply made my code work. – aylim14 Sep 04 '18 at 12:23
  • You're welcome! Now, get out there and write some code!!! :) – Joshua Schlichting Sep 04 '18 at 12:56
  • 1
    Yes I did! Finished this scipt along with some other improvements. With slow internet here, this’ll save me at least 10minutes each time i want to check the site - which is almost daily. Those add up! – aylim14 Sep 06 '18 at 03:51
  • Great! Take that 10 minutes a day, add up how much time that saves you over a year, and make sure your performance report reflects the man-hours you've saved via automation! Your management will be impressed and give you more things to automate :) – Joshua Schlichting Sep 06 '18 at 13:48
1

The input() returns a string, not an integer. So, if you are checking reply == 1 it will always fail, because your actual value is '1'. Replace your line with

reply = int(input())
blue_note
  • 27,712
  • 9
  • 72
  • 90
0

use:

while True:

This won't exit until:

break

is called

jack
  • 90
  • 1
  • 8