簡體   English   中英

如何在 selenium 腳本在 python 中運行時停止它,然后從頭開始重新啟動它?

[英]How do I stop a selenium script while it's running in python and then start it again from the begining?

我遇到的問題是,當我使用 driver.close() 停止腳本時,我無法重新開始腳本,因為實例與第一次嘗試相關聯。 有沒有辦法啟動一個新實例? 我正在使用 Tkinter 來啟動和停止 selenium 腳本。

    from tkinter import *
    from tkinter.ttk import *
    from SeleniumBot import SeleniumBot
    import threading
    
    root = Tk()
    root.title("SelBot")
    
    # Title of the program
    label = Label(text="SelBot")
    
    text = Text(root, height=5, width=52)
    loadbar = Progressbar(root, orient=HORIZONTAL, length=300, mode='indeterminate')
    s = SeleniumBot()
    
    
    def startBot():
        loadbar.start()
        text.insert(INSERT, 'Bot has started!\n')
        t1 = threading.Thread(target=sBot())
        t1.start()
    
    
    
    def sBot():
        s.login()
        text.insert(INSERT, 'Log In Complete-----------1/3\n')
        s.search()
        text.insert(INSERT, 'Search Complete-----------2/3\n')
        s.complete()
        text.insert(INSERT, 'Task Completed------------3/3\n')
        loadbar.stop()
    
    
    def endBot():
        text.insert(INSERT, 'Bot has stopped!\n')
        s.botStop()
        loadbar.stop()
    
    startBtn = Button(root, text="Start", command=startBot)
    stopBtn = Button(root, text="Stop", command=endBot)
    
    label.pack()
    startBtn.pack()
    stopBtn.pack()
    loadbar.pack()
    text.pack()
    
    root.mainloop()

我想出的解決我的問題的解決方案是將 selenium 機器人放在一個列表中。 這可能不是最好的解決方案,但它可以完成我想要的工作。 我使用列表的理由是,當單擊停止按鈕時,我可以停止 selenium 機器人,然后將其刪除並啟動一個新實例。 當我單擊開始按鈕時,它將使用新實例。

botList = [SeleniumBot()]


def startBot():
    loadbar.start()
    text.insert(INSERT, 'Bot has started!\n')
    t1 = threading.Thread(target=sBot)
    t1.start()



def sBot():
    botList[0].login()
    text.insert(INSERT, 'Log In Complete-----------1/3\n')
    botList[0].search()
    text.insert(INSERT, 'Search Complete-----------2/3\n')
    botList[0].complete()
    text.insert(INSERT, 'Task Completed------------3/3\n')
    loadbar.stop()


def endBot():
    text.insert(INSERT, 'Bot has stopped!\n')
    botList[0].botStop()
    botList.pop(0)
    botList.append(SeleniumBot())
    loadbar.stop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM