'How To use login and password from text file in python selenium to login gmail?

I am creating an automation to do something Like driver open a Gmail login URL. Get username and password from a text file. And do some task and close the driver. and after 5 sec open the Gmail Login URL again and read next username and password from the text file. text file format is

username1:password1 username2:password2

here is the code

with open('gmail.txt', 'r') as file:
       for details in file:
        gmailId, passWord = details.split(':')
        try:
            driver.get("https://accounts.google.com/signin")
            print('Opening URL')
            driver.implicitly_wait(15)
            #WebDriverWait(driver, 3)
            loginBox = driver.find_element_by_xpath('//*[@id ="identifierId"]')
            slow_typing(loginBox, gmailId)
            print('Entering Username')            
            nextButton = driver.find_elements_by_xpath('//*[@id ="identifierNext"]')
            nextButton[0].click()
            time.sleep(2)
            passWordBox = driver.find_element_by_xpath(
                '//*[@id ="password"]/div[1]/div / div[1]/input')
            slow_typing(passWordBox, passWord)
            print('Entering Password')
            nextButton = driver.find_elements_by_xpath('//*[@id ="passwordNext"]')
            nextButton[0].click()
          
            print('Login Successful...!!')
        except:
            pass
            print('Login Failed')
        
        time.sleep(2)
    driver.close()

the issue is its using the first username and password again and again after closing driver and opening again. any solution from experts?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source