简体   繁体   English

错误

[英]Python TKinter, Selenium and threading throwing driver not defined error

I got my program running but whenever I run the script from the button (minknapp2) TKinter stops responding until the script has finished.我的程序正在运行,但是每当我从按钮 (minknapp2) 运行脚本时,TKinter 都会停止响应,直到脚本完成。 I did some research and found out it's because I need to use threading, and I would be able to interact with TKinter while the script is running.我做了一些研究,发现这是因为我需要使用线程,并且我可以在脚本运行时与 TKinter 进行交互。

I have separated my program into 2 files which I am thinking is the issue.我已将我的程序分成 2 个文件,我认为这是问题所在。 one file runs TKinter and the second file runs my Selenium scripts.一个文件运行 TKinter,第二个文件运行我的 Selenium 脚本。 filename1 is NyceGui.py filename2 is webreader.py文件名 1 是 NyceGui.py 文件名 2 是 webreader.py

Tkinter file looks like this. Tkinter 文件如下所示。

from tkinter import *
import webreader as wr


root = Tk()


#Skapa labels
minLabel1 = Label(root, text = "Nyce automation Gui")
minLabel2 = Label(root, text = "Automatisera processer")
minLabel3 = Label(root, text = "Flytta artikel")
minLabel4 = Label(root, text = "Artikelnummer: ")
minLabel5 = Label(root, text = "Från: ")
minLabel6 = Label(root, text = "Till: ")

#Skapa knappar
minKnapp1 = Button(root, text = "Öppna Nyce", command=wr.startaNyce)
minKnapp2 = Button(root, text = "Bekräfta aktiv order", command=wr.bekräfta
minKnapp3 = Button(root, text = "Bekräfta småplock aktiv order", command=wr.bekräftaSmå)
minKnapp4 = Button(root, text = "AVBRYT SCRIPT", command=wr.stopp, bg ="red")

#Skapa textfält
mittText1 = Entry(root)
mittText2 = Entry(root)
mittText3 = Entry(root)

#Visa på skärm
minLabel1.grid(row=0, column=0)
minLabel2.grid(row=1, column=0)
minKnapp1.grid(row=2, column=0)
minKnapp2.grid(row=3, column=0)
minKnapp3.grid(row=4, column=0)
minKnapp4.grid(row=1, column=3)
minLabel3.grid(row=6, column=1)
minLabel4.grid(row=7, column=0)
mittText1.grid(row=7, column=1)
minLabel5.grid(row=8, column=0)
mittText2.grid(row=8, column=1)
minLabel6.grid(row=8, column=2)
mittText3.grid(row=8, column=3)




    root.mainloop()

And the Selenium file looks like this. Selenium 文件看起来像这样。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import sys



def startaNyce():
    global driver
    driver = webdriver.Chrome(executable_path=r'./chromedriver.exe')
    driver.get("http://awebpage.com")


def bekräfta():
    orange = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//input[contains(@class, 'nl-forms-wp-orange')]")))
    try:
        while orange:
            text = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[contains(@class, 'form-control nl-forms-wp-orange id-requisiteinfo-input-riLocation')]"))).get_attribute("value")
            search = driver.find_element_by_xpath("//input[contains(@value, 'Skanna lagerplats')]")
            search.send_keys(text)
            search.send_keys(Keys.RETURN)
            search2 = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//input[contains(@class, 'nl-forms-wp-orange id-requisiteinfo-input-riQuantity')]")))
            search.send_keys(Keys.RETURN)

    except KeyboardInterrupt:
        sys.exit()


def bekräftaSmå():
    orange = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//input[contains(@class, 'nl-forms-wp-orange')]")))
    try:
        while orange:
            text = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//input[contains(@class, 'nl-forms-wp-orange id-requisiteinfo-input-riLocation')]"))).get_attribute("value")
            search = driver.find_element_by_xpath("//input[contains(@value, 'Skanna lagerplats')]")
            search.send_keys(text)
            search.send_keys(Keys.RETURN)
            search.send_keys(Keys.RETURN)
            text = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//input[contains(@class, 'nl-forms-wp-orange')]"))).get_attribute("value")
            search2 = driver.find_element_by_xpath("//input[contains(@value, 'Skanna fack')]")
            search2.send_keys(text)
            search.send_keys(Keys.RETURN)

    except KeyboardInterrupt:
        sys.exit()

def stopp():
    sys.exit()

So I tried in my first code adding import threading所以我尝试在我的第一个代码中添加导入线程

and this block to test it out on (minknapp2)和这个块来测试它(minknapp2)

from tkinter import *
import webreader as wr
import threading


root = Tk()


#Skapa labels
minLabel1 = Label(root, text = "Nyce automation Gui")
minLabel2 = Label(root, text = "Automatisera processer")
minLabel3 = Label(root, text = "Flytta artikel")
minLabel4 = Label(root, text = "Artikelnummer: ")
minLabel5 = Label(root, text = "Från: ")
minLabel6 = Label(root, text = "Till: ")

#Skapa knappar
minKnapp1 = Button(root, text = "Öppna Nyce", command=wr.startaNyce)
minKnapp2 = Button(root, text = "Bekräfta aktiv order",   command=threading.Thread(target=wr.bekräfta).start())

But it throws this error.但它会抛出这个错误。

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Users\Python38-32\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "\webreader.py", line 17, in bekräfta
    orange = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//input[contains(@class, 'nl-forms-wp-orange')]")))
NameError: name 'driver' is not defined

Now I read somewhere that it's because I am starting a thread outside the main program.现在我在某处读到这是因为我在主程序之外启动了一个线程。 I am a bit clueless about this part, so any help would be much appreciated.我对这部分有点一无所知,所以任何帮助将不胜感激。

Very simple fix, just remove the () from the command.非常简单的修复,只需从命令中删除 ()。

minKnapp2 = Button(root, text = "Bekräfta aktiv order",   command=threading.Thread(target=wr.bekräfta).start)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM