我目前正在使用 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'
uj5u.com熱心網友回復:
我已經完成了你的代碼..... 你犯了一個非常常見的錯誤,你使用 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
謝謝
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/398349.html
標籤:Python 硒 硒网络驱动程序 特金特 tkinter-text
下一篇:使用硒加載網頁回傳錯誤
