0

I'm having issues when I import a module that contains tkinter in it. I don't want to paste the code because it's super long and it not my problem here, I've created a module using tkinter and selenium, this one opens a web browser and take an snapshot from a captcha code, store it in my local desk and then show it in a tkinter window, when I execute this function within the module, it works perfectly without issues, the problem began when I imported this module into the main module where I want to use this function, when I execute it, the web browser starts perfectly, the snapshot is taken, and then when the window should pop up...nothing happen, the code doesn't stop working, any error is raised...I've been looking for answers to this missbehavior, but didn't find anything like this

from selenium import webdriver
import tkinter as tk
from PIL import Image, ImageTk
# from tkinter import filedialog
from pathlib import Path

# webDriv_path = Path(__file__).parent.__str__()
def getCaptchaImg(driver, path):
    captchaImg = driver.find_element_by_css_selector("#ValidateCodeServlet")
    captchaImg.screenshot(path + "\captcha_img.png")


def captchaInterface(path):
    
    def closeWindow():
        top.destroy()

    def getCaptcha():
        captchaCode.set(captchaBox.get())
        closeWindow()

    top = tk.Tk()
    
    top.title("Captcha code")
    captchaCode = tk.StringVar()
    image = Image.open(path + "\captcha_img.png")
    my_img = ImageTk.PhotoImage(image)
    my_img_lab = tk.Label(image=my_img)
    captchaBox = tk.Entry(width=13)  # captcha box creation
    submitButt = tk.Button(top, text="Go!", width=10, height=3, command=getCaptcha)  # submit butt creation
    my_img_lab.place(x=5, y=0)  # Captcha image pack
    captchaBox.place(x=6, y=37)  # Captcha box pack
    submitButt.place(x=95, y=2)  # submit butt pack
    top.geometry("180x70+750+450")
    top.attributes("-toolwindow", True)
    top.resizable(width=False, height=False)
    
    top.mainloop()
    
    return captchaCode.get()


def submitFileInterface():
    def closeWindow():
        top.destroy()

    def getFilePath():
        cspFilePath.set(tk.filedialog.askopenfile(filetypes=[("Excel file", ".csv")]).name)
        cspFile.set(cspFilePath.get().split('/')[-1])

    top = tk.Tk()
    top.title("Submit file")
    cspFile = tk.StringVar(top)
    cspFilePath = tk.StringVar(top)
    fileSel = tk.Label(top, text="Selected File: ")
    fileSelShow = tk.Label(top, textvariable=cspFile)
    openFileButt = tk.Button(top, text="Select File", width=10, command=getFilePath)
    submitButt = tk.Button(top, text="Submit!", width=10, command=closeWindow)
    fileSel.place(x=5, y=1)
    fileSelShow.place(x=45, y=20)
    openFileButt.place(x=6, y=47)
    submitButt.place(x=95, y=47)
    top.geometry("180x75+750+450")
    top.attributes("-toolwindow", True)
    top.resizable(width=False, height=False)
    top.mainloop()
    return cspFilePath.get()

def cspLogin(path):
    # Configuring Browser
    options = webdriver.ChromeOptions()
    options.add_experimental_option('excludeSwitches', ['enable-logging'])
    # options.headless = True
    # Settig chromedriver Path
    wd_path = path + "\chromedriver.exe"
    csp_user = 'user'
    csp_pass = 'pass'
    brwsr = webdriver.Chrome(executable_path=wd_path, options=options)
    # Open CSP
    brwsr.get('web')
    usr_input = brwsr.find_element_by_xpath('//*[@id="username"]')
    usr_input.clear()
    usr_input.send_keys(csp_user)
    pass_input = brwsr.find_element_by_xpath('//*[@id="password"]')
    pass_input.clear()
    pass_input.send_keys(csp_pass)
    # inserting captcha
    getCaptchaImg(brwsr, path)
    captcha_code = captchaInterface(path)
    captcha_input = brwsr.find_element_by_xpath(
        '//*[@id="content-table"]/tbody/tr/td[2]/table[2]/tbody/tr/td[1]/table[2]/tbody/tr[2]/td/table/tbody/tr/td/form/table/tbody/tr[2]/td[2]/table/tbody/tr[3]/td[2]/input')
    captcha_input.clear()
    captcha_input.send_keys(captcha_code)
    brwsr.find_element_by_xpath(
        '//*[@id="content-table"]/tbody/tr/td[2]/table[2]/tbody/tr/td[1]/table[2]/tbody/tr[2]/td/table/tbody/tr/td/form/table/tbody/tr[3]/td[2]/table/tbody/tr/td/a').click()
    brwsr.find_element_by_xpath('//*[@id="navigation"]/table[4]/tbody/tr[2]/td/a').click()
    brwsr.find_element_by_xpath('//*[@id="emailAdd"]').send_keys(csp_user)
    brwsr.find_element_by_xpath('//*[@id="batchType"]/option[3]').click()
    cspFile = submitFileInterface()
    subFile = brwsr.find_element_by_xpath(
        '//*[@id="content-table"]/tbody/tr/td[2]/table[2]/tbody/tr/td[1]/table[2]/tbody/tr[2]/td/table/tbody/tr/td/form/table/tbody/tr[2]/td[2]/table/tbody/tr/td[1]/label/input')
    subFile.send_keys(cspFile)
    goButton = '//*[@id="content-table"]/tbody/tr/td[2]/table[2]/tbody/tr/td[1]/table[2]/tbody/tr[2]/td/table/tbody/tr/td/form/table/tbody/tr[5]/td[2]/table/tbody/tr[2]/td[1]/a'
    brwsr.find_element_by_xpath(goButton).click()
    brwsr.quit()




# cspLogin(webDriv_path)

and this is the module where I'm trying to use it

import os
from pathlib import Path
import time
import getpass
import json
from shutil import copy2
import datetime as dt
from csp_dwnld import *


# Program created to test if a python based windows app can work with an external txt

base_path = Path(__file__).parent.__str__()
dirTxtLoc = base_path + "\settings.txt"  # Directory is stored in a txt file
os.system("mode con: cols=120")



def showMenu():
    topRefresh("*** Welcome to quoting assistant *** ".center(120))
    wDir = loadFolderPath(dirTxtLoc)  # Working Directory
    checkFiles()
    print("1 - Set/Change Directory folder Location")
    print("2 - Set csp login")
    print("3 - Create new folder")
    print("4 - Submit csv to csp")
    print("\n\n\n\n")
    print("9 - Help")
    print("0 - Exit\n")
    option = input("Select an option: ")
    print('\n')
    # while option != "0":
    optSelection(option, wDir, base_path)
    print("\n")
    # showMenu()

def optSelection(option, path, alt_path):
    if option == "1":
        setDirectory()
    elif option == "2":
        setCsp()
    elif option == "3":
        createFolder(path)
    elif option == "4":
        cspLogin(alt_path)
    elif option == "9":
        optHelp()
    elif option == "0":
        os.system("exit")
leo_bttf
  • 11
  • 1

0 Answers0