使用 Tinker 获取用户选择的信息和驱动程序
Using Tinker to get User selected Information and drivers
我目前正在使用 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 作为 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
谢谢
我目前正在使用 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 作为 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
谢谢