繁体   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