Home > database >  Using Tinker to get User selected Information and drivers
Using Tinker to get User selected Information and drivers

Time:12-31

I am currently using Tkinter, openpxl, and selenium. To create a GUI that takes user inputs and then using selenium and openpxl scrapes the website for information and then imports them into a document. Currently my GUI is working as expecting.

The header files that I plan on using for this small project are these:

import tkinter as tk
from tkinter import filedialog as fd
from tkinter import ttk
from tkinter.messagebox import showerror, showinfo
from openpyxl import load_workbook
from selenium import webdriver

My code is a bit complex and shitty because I'm just learning as I go and trying to understand how higher-level programming works. I'm a junior in college studying Electrical engineering not CS major. So any big no-nos I did would be appreciated to be pointed out in a simple manner. I'm not to familiar with the lingo that is typically used.

def file_lookup():
    filetypes = (
        ('All files', '*.*'),
        ('Application files', '*.exe'),
        ('Excel files', '*.xlsx')
    )
    filename = fd.askopenfilename(
        title='Insert File',
        initialdir='/',
        filetypes=filetypes)
    if filename != "":
        showinfo(
            title='Selected File',
            message=filename
        )
    return filename
       
def selenium(array_product_name, webdriver):
    print(array_product_name)
    print(webdriver)
    web = webdriver
    # main website of MTE Corporation
    main_page = "https://www.mtecorp.com/click-find/"
    driver = webdriver.Chrome(executable_path=web)
    # Goes to main page
    driver.get(main_page)
    # prints page title
    print(driver.title)
    # to maximize the browser window
    driver.minimize_window()


class MainFrame(ttk.Frame):
    # Initialization
    def __init__(self, container):
        super().__init__(container)
        # field options
        options = {'padx': 5, 'pady': 5}

        # file_name
        self.filename = str()

        # web driver location
        self.web_driver = str()

        # settings button
        self.button = ttk.Button(self, text="Finished Setting", command=self.settings)
        self.button.grid(row=9, column=3, sticky=tk.EW, **options)

        # excel button
        self.exel_button = ttk.Button(self, text='Insert Excel File', command=self.select_excel)
        self.exel_button.grid(column=0, row=9, sticky=tk.EW, **options)

        # web driver button
        self.web_button = ttk.Button(self, text='Select Web driver', command=self.select_webdriver)
        self.web_button.grid(column=1, row=9, sticky=tk.EW, **options)

        # add padding to the frame and show it
        self.grid(padx=10, pady=10, sticky=tk.NSEW)

    def settings(self):
        self.check_entry


    @property
    def check_entry(self):
        check = True
        # Retrieve the of name, date, and MTE Selection
        inputs = [self.projectname.get(), self.Date.get(), self.selected_product.get(), self.filename,
                  self.web_driver]
        print(inputs)
        if inputs[0] == '':
            showerror(
                title='Error-Name',
                message='Please type in name.'
            )
            check = False
        if inputs[1] == '':
            showerror(
                title='Error-Date',
                message='Please type in Date.'
            )
            check = False
        if inputs[2] == '':
            showerror(
                title='Error-Selection',
                message='User did not Selected Product Line, Please Check.'
            )
            check = False
        if inputs[3] == '':
            showerror(
                title='Error-Excel File',
                message='User did not Inserted an Excel File.'
            )
            check = False
        if inputs[4] == '':
            showerror(
                title='Error-Webdriver',
                message='User did not Inserted a Selenium based Webdriver.'
            )
            check = False
        if check:
            showinfo(
                title='Success',
                message='Settings are successful'
            )
            self.show_selected_product()
            product_name = load_excel(inputs[3])
            selenium(product_name, inputs[4])

    def select_excel(self):
        self.filename = file_lookup()

    def select_webdriver(self):
        self.web_driver = file_lookup()
            

Currently, the error that I'm in countering evolves using the user's selected selenium webdriver and opening a specific webpage. In the future, my goal for this is so whenever someone else is using this GUI on their computer they can just selected their chrome driver instead of having to change the main code and information. I have a feeling that the error is due to the user input webdrive is cannot be accessed to selenium libraries making it just crash but again idk.

THIS IS THE ERROR:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\jjurado\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:\Users\jjurado\Documents\PYTHON\GUI_MTE_PRODUCT_SEARCH\GUI..py", line 153, in settings
    self.check_entry
  File "C:\Users\jjurado\Documents\PYTHON\GUI_MTE_PRODUCT_SEARCH\GUI..py", line 205, in check_entry
    selenium(product_name, inputs[4])
  File "C:\Users\jjurado\Documents\PYTHON\GUI_MTE_PRODUCT_SEARCH\GUI..py", line 65, in selenium
    driver = webdriver.Chrome(executable_path=web)
AttributeError: 'str' object has no attribute 'Chrome'

CodePudding user response:

I had gone through through your code..... you had done a very common mistake you had used webdriver as a argument of your selenium function but you cannot do so as you have already imported webdriver from selenium.....

Error in code:

def selenium(array_product_name, webdriver):
print(array_product_name)
print(webdriver)
web = webdriver
# main website of MTE Corporation
main_page = "https://www.mtecorp.com/click-find/"
driver = webdriver.Chrome(executable_path=web)

Answer:

change the webdriver argument as web...

def selenium(array_product_name, web):
print(array_product_name)
print(web)
# main website of MTE Corporation
main_page = "https://www.mtecorp.com/click-find/"
driver = webdriver.Chrome(executable_path=web)
# Goes to main page
driver.get(main_page)
# prints page title
print(driver.title)
# to maximize the browser window
driver.minimize_window()

change this part of your code just replace webdriver with web and erase web=webdriver

Thanks

  • Related