Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
506 views
in Technique[技术] by (71.8m points)

python - PyQt QProgressBar not working when I use it with Selenuim

I built a PyQt5 GUI to do some Selenium testing. Everything works as expected, except for the PyQt progress bar.

In the first example below, where I use the Selenium browser, the progress bar just jumps to 100%, at the end, when the browser closes. But, the Selenium works as expected.

def test(self):
        self.completed = 0
        browser = webdriver.Firefox()
        links = ['http://www.somesite.com/', 'http://www.somesite.com/page2',
                 'http://www.somesite.com/page3']
        for link in links:
            browser.get(link)
            self.completed += 100 / len(links)
            time.sleep(2)
            print(link)
            self.progressBar.setValue(self.completed)
        browser.close()

But, in this version below, with the Selenium browser commented out, the progress bar works as expected.

def test(self):
        self.completed = 0
        #browser = webdriver.Firefox()
        links = ['http://www.somesite.com/', 'http://www.somesite.com/page2',
                 'http://www.somesite.com/page3']
        for link in links:
            #browser.get(link)
            self.completed += 100 / len(links)
            time.sleep(2)
            print(link)
            self.progressBar.setValue(self.completed)
        #browser.close()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The blocking tasks are not friendly with the event loop where the GUI is executed as they prevent the normal tasks that the GUI performs such as ticket checking, redrawing, etc. from being executed.

The solution in these cases is to use thread to execute the blocking task and use the signals to send the information.

import sys

from PyQt5 import QtCore, QtWidgets

from selenium import webdriver

class SeleniumWorker(QtCore.QObject):
    progressChanged = QtCore.pyqtSignal(int)
    def doWork(self):
        progress = 0
        browser = webdriver.Firefox()
        links = ['http://www.somesite.com/',
        'http://www.somesite.com/page2',
        'http://www.somesite.com/page3']
        for link in links:
            browser.get(link)
            progress += 100 / len(links)
            self.progressChanged.emit(progress)
        browser.close()

class Widget(QtWidgets.QWidget):
    def __init__(self, *args, **kwargs):
        QtWidgets.QWidget.__init__(self, *args, **kwargs)
        lay = QtWidgets.QHBoxLayout(self)
        progressBar = QtWidgets.QProgressBar()
        progressBar.setRange(0, 100)
        button = QtWidgets.QPushButton("Start")
        lay.addWidget(progressBar)
        lay.addWidget(button)
        self.thread = QtCore.QThread()
        self.worker = SeleniumWorker()
        self.worker.moveToThread(self.thread)
        self.thread.started.connect(self.worker.doWork)
        button.clicked.connect(self.thread.start)
        self.worker.progressChanged.connect(progressBar.setValue, QtCore.Qt.QueuedConnection)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...