Looping a Selenium Python script
Question: I have the following script that opens a browser and logs in then closes the
browser:

    
    
    from selenium import webdriver
    browser=webdriver.Ie()
    import time
    
    x=4
    for i in range(x):    
        browser.get("http://localhost:8080/customercareweb-prototype/")
        username=browser.find_element_by_xpath(".//*[@placeholder='Username']").send_keys("op1")
        password=browser.find_element_by_xpath(".//*[@placeholder='Password']").send_keys("op1")
        time.sleep(2)
        browser.find_element_by_xpath(".//*[@class='dlg-auth-button z-button']").click()
        browser.close()
    

When i run this i get the error: `TypeError: string indices must be integers`

What am i doing wrong here. I just want to get this script to run for 4 times
in a row.


Answer: I managed to get it to work wrapping it i a function. But i am still puzzled
why i could not do it with simple "for" statement.

    
    
    def login():
    browser=webdriver.Ie()    
    browser.get("http://localhost:8080/customercareweb-prototype/")
    username=browser.find_element_by_xpath(".//*[@placeholder='Username']").send_keys("op1")
    password=browser.find_element_by_xpath(".//*[@placeholder='Password']").send_keys("op1")
    time.sleep(2)
    browser.find_element_by_xpath(".//*[@class='dlg-auth-button z-button']").click()
    
    i=login()
    for i in range(1):
    login()
    

