简体   繁体   中英

PyQT : How to pass a webdriver object from QThread to UI thread?

I'm making a program that automatically enters to a website and login(basically using selenium and chrome webdriver). User can type own information(id&pw) and site address in a dialog(used PyQt4 module). After finishing it, pressing ok button will execute it.

After logging in, I want to do some other actions with that webdriver objects.

So my question is, how can I pass the webdriver object to the main thread(which is UI thread here) so that I can do some other actions(such as logging out, etc), or how to manage that webdriver object generated in other thread in main thread?

I'm using Python3.7.4 and PyQt4 version.

I googled similar questions and found that it might be related with signal&slots. so I tried to imitate this example ( https://nikolak.com/pyqt-threading-tutorial/ ) which uses custom signal. In this example, it passes a QString instance to the main thread(UI thread). So I tried to pass my webdriver object by imitating it, but It's not going well...

The basic structure of code is following:

class MyDialog(QDialog):
    def __init__(self):
        QDialog.__init__(self)
        # and some codes with widget layouts

    def btnOkClicked(self):
        a = [self.editSite1.text(), self.editId.text(), self.editPw.text()]

        self.gothread = goWebsiteThread(a)
        # 'goWebsiteThread' is a thread that generates webdriver object and executes login function

        self.connect(self.gothread, SIGNAL("add_driver(PyQt_PyObject)"), self.add_driver)
        # this line is what I tried to pass the driver object to this main thread

        self.gothread.start()


class goWebsiteThread(QThread, QObject):
# I tried to pass this class's object by making this class inherit QObject class... sorry for unfounded try..

    def __init__(self, sites):
        QThread.__init__(self)
        self.sites = sites

    def goWebsite(self):
        self.driver = webdriver.Chrome('./chromedriver.exe', options=options)
        self.driver.get(some site address that user typed)
        # and codes to log in

        self.emit(SIGNAL('add_driver(PyQt.PyObject)'), self.driver)
        # I tried to pass the driver object by emitting signal...

    def run(self):
    self.goWebsite()

But this doesn't work (MyDialog object doesn't recognize the driver object).

How can I properly pass the webdriver object to MyDialog object and use it?

Webdriver should run on a new thread and it would be impossible to control webdriver from a UI thread. But you can still store the webdriver as a member variable of the UI instance. If you want to send some commands to the webdriver, you would need to start a new thread and handle automation works in the newly created thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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