Google authentication TOTP error

Auriniumus
Auriniumus edited December 2024 in Python client
Hello!
I have been doing web-scraping using selenium to automate logging to my zerodha kite account based on this youtube video as my reference (). I made some changes here and there (for example correcting the reference ids of elements on zerodha kite login pages) and there was absolutely no problem in automating the log in for almost a month now. However, I had to update my account password and hence google totp yesterday due to security reasons. I have updated the google totp secret key in my apikey.txt file but suddenly it's not working anymore. Whenever I try running my python code, it opens the chrome window, fills username, password correctly, moves onto totp page, tries entering a totp, fails, and the window quits. Python terminal shows following error:


DevTools listening on ws://127.0.0.1:52555/devtools/browser/9ff9d9a3-6853-4f52-ba2e-66025f6345c7
Traceback (most recent call last):
File "d:\Algo\.venv\Scripts\trade_placement_birbull", line 482, in
autologin()
File "d:\Algo\.venv\Scripts\trade_placement_birbull", line 55, in autologin
request_token=driver.current_url.split('request_token=') [1] [:32]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^
IndexError: list index out of range

Here's my code:

def autologin():

token_path = "apikey.txt"
key_secret = open(token_path, 'r').read().split()
kite = KiteConnect(api_key=key_secret[0])
chrome_driver_path = "D:/Algo/chromedriver.exe"
chrome_options = Options()
chrome_service = Service(executable_path=chrome_driver_path)

#options.add_argument('--headless')

driver = webdriver.Chrome(service=chrome_service, options=chrome_options)
driver.get(kite.login_url())
driver.implicitly_wait(7)
username = driver.find_element(By.XPATH, '//input[@id="userid"]')
password = driver.find_element(By.XPATH, '//input[@id="password"]')
username.send_keys(key_secret [2])
password.send_keys(key_secret [3])
driver.implicitly_wait(10)
driver.find_element(By.XPATH, '//*[@id="container"]/div/div/div/form/div[4]/button').click()
driver.implicitly_wait(10)
time.sleep(2)
pin = driver.find_element(By.XPATH, '/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[1]/input')
totp = TOTP (key_secret[4])
token = totp.now()
pin.send_keys(token)
#pin.send_keys(key_secret [4])
driver.find_element(By.XPATH, '/html/body/div[1]/div/div/div[1]/div[2]/div/div/form/div[2]/button').click()
time.sleep(5)
request_token=driver.current_url.split('request_token=') [1] [:32]
with open('request_token.txt', 'w') as the_file:
the_file.write(request_token)

driver.quit()
request_token= open("request_token.txt",'r').read()
key_secret = open("apikey.txt",'r').read().split()
kite = KiteConnect(api_key=key_secret[0])
data = kite.generate_session(request_token, api_secret=key_secret [1])
with open('access_token.txt', 'w') as file:
file.write(data["access_token"])
print("Auto login Completed")
Tagged:
  • sujith
    It is mandatory as per the regulations that a user has to login manually at least once a day. We don't recommend automating login.
This discussion has been closed.