I am working on program that changes user passwords on a website we use at work. The problem I have run into is half of the accounts have been moved to login via email instead of username. If the account has been moved to login via email the user has to change their password via email. So the program I have written is supposed to detect if the account has been moved over or if the admin account was able to change the password. I did this with a Try / While loop. Everything seems to work expect sending a email if the account has been switched over. The other issue I am running into is after the loop is completed it will try to run it again. Please see the code below and let me know if you have any questions.
import time
from hashlib import new
from multiprocessing.connection import wait
from select import select
from tkinter import E
from webbrowser import BaseBrowser, Chrome
from xml.etree.ElementPath import find
from xmlrpc.client import boolean
import win32clipboard
import win32com.client as win32
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException, TimeoutException
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
import ADconnect
def ChangeLogin(AdminUser, AdminPassword):
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome (options=options)
driver.maximize_window()
driver.get('https://power.dat.com')
def LoginPass():
Logon = driver.find_element(By.XPATH, '//*[@id="mat-input-1"]')
Logon.send_keys(AdminUser)
password = driver.find_element(By.XPATH, '//*[@id="mat-input-0"]')
password.send_keys(AdminPassword)
loginbutton = driver.find_element(By.XPATH, '//*[@id="submit-button"]')
loginbutton.click()
(time.sleep(3))
LoginPass()
def AdminBtn():
AD = "https://power.dat.com/admin/office"
driver.execute_script("window.open(' ');")
driver.switch_to.window(driver.window_handles[1])
driver.get(AD)
AdminBtn()
WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="search"]'))).send_keys(ADconnect.email)
(time.sleep(3))
UsrPro = driver.find_element(By.XPATH, '//td[7]')
UsrPro.click()
#Password
password = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='password']")))
password.clear()
password.send_keys("totalyDude")
#Confirm Password
ConPass = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='confirmPassword']")))
ConPass.clear()
ConPass.send_keys("totalyDude")
#Copy Username
UserName = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='loginId']")))
UserName.send_keys(Keys.CONTROL, 'a')
UserName.send_keys(Keys.CONTROL, 'c')
#Save Button
SaveBTN= WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="data"]/div[3]/table/tbody/tr/td[1]/ng-form/div/button[2]')))
SaveBTN.click()
driver.quit()
def TstAcct():
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver1 = webdriver.Chrome (options=options)
driver1.maximize_window()
driver1.get('https://power.dat.com')
#Calling Username in clipboard
win32clipboard.OpenClipboard()
NewUserName = win32clipboard.GetClipboardData()
(time.sleep(3))
#Send username
driver1.find_element(By.XPATH, '//*[@id="mat-input-1"]').send_keys(NewUserName)
#Send Password
driver1.find_element(By.XPATH, '//*[@id="mat-input-0"]').send_keys('total2')
def TstAcct():
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver1 = webdriver.Chrome (options=options)
driver1.maximize_window()
driver1.get('https://power.dat.com')
#Calling Username in clipboard
win32clipboard.OpenClipboard()
NewUserName = win32clipboard.GetClipboardData()
(time.sleep(3))
#Send username
driver1.find_element(By.XPATH, '//*[@id="mat-input-1"]').send_keys(NewUserName)
#Send Password
driver1.find_element(By.XPATH, '//*[@id="mat-input-0"]').send_keys('totalyDude')
#Click Login
# driver1.find_element(By.XPATH, '//*[@id="submit-button"]').click()
#Get User name that is logged in
# confirm = (WebDriverWait(driver1, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[@id='user-salutation']"))).get_attribute("innerHTML"))
# print(confirm)
#Check Username Matches and send email if it does
try:
driver1.find_element(By.XPATH, '//*[@id="submit-button"]').click()
while WebDriverWait(driver1, 20).until(EC.presence_of_element_located((By.XPATH, "//div[@class='error-content']"))):
time.sleep(3)
# WebDriverWait(driver1, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='error-content']"))).click()
driver1.find_element(By.XPATH, "//a[@id='password-reset']").click()
driver1.find_element(By.XPATH, "//span[normalize-space()='CONTINUE']").click()
outlook1 = win32.Dispatch('outlook.application')
mail = outlook1.CreateItem(0)
mail.To = ADconnect.email
mail.Subject = "DAT Account Password Change"
mail.Body = (ADconnect.FirstN + " your DAT account has been transitioned over to email login. You should receive a email from DAT with a link to change your password. \n \n" + "Please see the log in info below:\n \n"
"Username: " + ADconnect.email + "\nPassword: total2 \n\n"
"You can log in with the below link \n"
"https://power.dat.com")
print("Email sent")
break
except:
print("issue occurred")
try:
confirm = (WebDriverWait(driver1, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[@id='user-salutation']"))).get_attribute("innerHTML"))
while confirm == ("Hi " + NewUserName):
print(confirm)
print("Users password has been changed")
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = ADconnect.email
mail.Subject = "DAT Account Password Change"
mail.Body = (ADconnect.FirstN + " your DAT account password has been changed!\n \n" + "Please see the log in info below:\n \n"
"Username: " + NewUserName + "\nPassword: total2 \n\n"
"You can log in with the below link \n"
"https://power.dat.com")
mail.Send()
break
except:
print("issue occurred")
finally:
driver1.quit()
TstAcct()
Below is the other file that is being called by the above file:
import email
from multiprocessing import connection
from sqlite3 import Cursor, Row
from tabnanny import check
from types import NoneType
import pyodbc
import pandas as pd
from psycopg2 import sql
import ADconnect
import PassChngWeb
def chckAcct():
SQLEmail = ADconnect.email
Hostname = 'ServerTime'
IP_Address = '111.11.1.11'
Driver_Name = 'SQL SERVER'
Server_Name = "ServerTime"
DATABASE_NAME = 'TimeToParty'
connection_STRING = f"""
DRIVER= {{{Driver_Name}}};
SERVER={Server_Name};
DATABASE={DATABASE_NAME};
Trust_Connection=yes;
"""
conn = pyodbc.connect(connection_STRING)
print(conn)
cursor = conn.cursor()
check = cursor.execute("SELECT Account_Number FROM DATacctTQLfin WHERE Email_Address = ?", (SQLEmail) )
while check is None:
import WebDAT
print("No user account found")
WebDAT.CreateAcct()
WebDAT.sqlInsert()
break
else:
for x in cursor.fetchone():
var = x
print(var)
check = cursor.fetchone()
global AdminUser
global AdminPassword
if check is None:
import WebDAT
print("No user account found")
AdminUser = '0'
AdminPassword = '0'
WebDAT.CreateAcct()
for x in check:
var = x
print(var)
if x == ('jsparky'):
print("Username: JSparky")
AdminUser = "jsparky"
AdminPassword = "IMaLostBoy"
PassChngWeb.ChangeLogin(AdminUser, AdminPassword)
if x == ("JSparky2/"):
print(x)
print("Username: JSparky2")
AdminUser = "JSparky2"
AdminPassword = "StillALostBoy"
PassChngWeb.ChangeLogin(AdminUser, AdminPassword)
if x == ("jazzadmin"):
print(x)
print("Username: jazzadmin")
AdminUser = "jazzadmin"
AdminPassword = ("JazzHands")
PassChngWeb.ChangeLogin(AdminUser, AdminPassword)
if x == ("adminMean"):
print(x)
print("Username: adminMean")
AdminUser = "adminMean"
AdminPassword = ("TooTheFloor")
PassChngWeb.ChangeLogin(AdminUser, AdminPassword)
if x == ("RogerDat"):
print(x)
print("Username: RogerDat")
AdminUser = "RogerDat"
AdminPassword = ("ToTheWall")
PassChngWeb.ChangeLogin(AdminUser, AdminPassword)
if x == ("LukeSkyWalker"):
print(x)
print("Username: LukeSkyWalker")
AdminUser = "LukeSkyWalker"
AdminPassword = ("TillTheSweat")
PassChngWeb.ChangeLogin(AdminUser, AdminPassword)
if x == ("SuperKlein"):
print(x)
print("Username: SuperKlein")
AdminUser = "SuperKlein"
AdminPassword = ("DropsDownMy")
PassChngWeb.ChangeLogin(AdminUser, AdminPassword)
if x == ("NoDetention/"):
print(x)
print("Username: NoDetention")
AdminUser = "NoDetention"
AdminPassword = ("Ballzzzz")
PassChngWeb.ChangeLogin(AdminUser, AdminPassword)
if x == ("Vader"):
print(x)
print("Username: Vader")
AdminUser = "Vader"
AdminPassword = ("IamYouFather")
PassChngWeb.ChangeLogin(AdminUser, AdminPassword)
if x == ("R2D2/"):
print(x)
print("Username: R2D2")
AdminUser = "R2D2"
AdminPassword = ("R2D2")
PassChngWeb.ChangeLogin(AdminUser, AdminPassword)
if x == ("BabyYoda"):
print(x)
print("Username: BabyYoda")
AdminUser = "BabyYoda"
AdminPassword = ("FalconX")
PassChngWeb.ChangeLogin(AdminUser, AdminPassword)
cursor.close()
conn.close()