【问题标题】:Using Tinker to get User selected Information and drivers使用 Tinker 获取用户选择的信息和驱动程序
【发布时间】:2021-12-30 16:55:02
【问题描述】:

我目前正在使用 Tkinter、openpxl 和 selenium。创建一个接受用户输入的 GUI,然后使用 selenium 和 openpxl 从网站上抓取信息,然后将它们导入到文档中。目前我的 GUI 正在按预期工作。

我计划用于这个小项目的头文件是:

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

我的代码有点复杂和糟糕,因为我只是边学边学,并试图理解更高级别的编程是如何工作的。我是一名大三学生,学习的是电气工程而不是 CS 专业。因此,如果我以简单的方式指出我所做的任何重大禁忌,我将不胜感激。我对常用的术语不太熟悉。

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()
            

目前,我正在反击的错误是使用用户选择的 selenium webdriver 并打开特定网页而演变的。将来,我的目标是,每当其他人在他们的计算机上使用这个 GUI 时,他们就可以选择他们的 chrome 驱动程序,而不必更改主要代码和信息。我有一种感觉,错误是由于用户输入 webdrive 无法访问 selenium 库,使其只是崩溃,但又是 idk。

这是错误:

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'

【问题讨论】:

  • 显然webdriver是一个字符串,你也应该提供一个minimal reproducible example而不是你的整个代码,也不是如何使用property装饰器,只是让它成为一个实例方法跨度>
  • 感谢您的回复,在您的帮助下,我发现我犯了一个愚蠢的错误并使用了 selenium 使用的特定关键字。感谢您的回复!

标签: python selenium selenium-webdriver tkinter tkinter-text


【解决方案1】:

我已经浏览了你的代码..... 你犯了一个非常常见的错误,你使用 webdriver 作为你的 selenium 函数的参数,但你不能这样做,因为你已经从 selenium 导入了 webdriver.....

代码错误:

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)

答案:

将 webdriver 参数更改为 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()

更改这部分代码只需将 webdriver 替换为 web 并删除 web=webdriver

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多