Home > Software design >  browser shuts down after function being executed for the second time
browser shuts down after function being executed for the second time

Time:06-12

I'm using python and selenium to try to log in on a website and it works however after 8 attemps I've made it so that it closes the browser, does something thats irrelevant to this issue, and re-opens the browser with same exact function as it does on the startup. This is the chunk of code

file1 = open('C:/Users/miki2/OneDrive/Desktop/script/test.txt', 'r')
lcount = 0
count = 0
ccoun = 1
def lsc():
    global lcount, count, ccoun
    Lines = file1.readlines()[lcount:]
    count = 0
    print('count = '   str(count))
    for line in Lines:
        count  = 1
        lcount  = 1
        if count == 9 :
            # Close the browser
            browser.quit()
            print('browser.quit in if count == 9')

On the startup it works fine but when it comes to closing and re-opening it works up until this:

print('count = '   str(count))

It prints out the count = 0 as it should and then browser shuts down as if it crashes or something so it lead me to believe that maybe this:

for line in Lines:
    count  = 1
    lcount  = 1
    if count == 9 :
        # Close the browser
        browser.quit()
        print('browser.quit in if count == 9')

closes / crashes the browser. Note that the browser.quit() that is under if statement doesn't get called because it doesn't print that "debug" under it.

lcount is line count, it should be telling the code to not start over from line 1 but to start for loop from last line that was done before browser was shut down, adding [lcount:] after readlines() is not my idea I saw it online somewhere that it should work that way.

Any idea on why browser would shut down?

CodePudding user response:

Nothing happens within your loop - your print statement (along with the rest of your code) is outside of it. This is how I would rewrite your code:

def lsc():
    global lcount
    with open('C:/Users/miki2/OneDrive/Desktop/script/test.txt', 'r') as f:
         for count, line in enumerate(list(f)[lcount:lcount 9]):
             print("count = "   str(count))
             lcount  = 1
         browser.quit()

list(f)[lcount:lcount 9] gets the lines from lcount to lcount 9 and enumerate returns an iterator of (index, line) tuples.

CodePudding user response:

I recreated and ran your code and it works just as designed. The only time you will not get to print('browser.quit in if count == 9') is if the file you're reading is empty or does not at least have 9 lines.

from selenium import webdriver

browser = webdriver.Chrome()
file1 = open('./test.txt', 'r')
lcount = 0
count = 0
ccoun = 1

def lsc():
    global lcount, count, ccoun
    Lines = file1.readlines()[lcount:]
    count = 0
    print('count = '   str(count))
    for line in Lines:
        count  = 1
        lcount  = 1
        if count == 9 :
            browser.quit()
            print('browser.quit in if count == 9')

lsc()
  • Related