PyQt QProgressBar not working when I use it with Selenuim


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()





run the GUI on a different thread.
– Florent B.
Apr 3 at 13:37





@FlorentB. The way my project is structured, I don't think I can wrap my head around that. Can you please share some more details?
– Joe T. Boka
Apr 3 at 13:45




1 Answer
1



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_())





Thanks for this. This works great! But, unfortunately, I still can't use it because I need to use the progress bar for several different functions. Not just one. Can you please advise me on how I can use this solution for other lists in other methods?
– Joe T. Boka
Apr 4 at 3:34





@JoeT.Boka You understand that your current requirement is a little confusing, I could help you if you provide a Minimal, Complete, and Verifiable example, in addition to a clearer explanation as for example what percentage represents the execution of each function.
– eyllanesc
Apr 4 at 3:39





Thanks. I will create and provide a new example that will demonstrate the current problem more clearly. I will let you know when the new question is posted. Thanks again.
– Joe T. Boka
Apr 4 at 3:53





I posted an example of the current problem as discussed. stackoverflow.com/questions/49647205/… Hope it makes sense. Please take a look.
– Joe T. Boka
Apr 4 at 9:20






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to input without newline? (Python)

C++ thread error: no type named ‘type’ MINGW

Analog for TagView in flutter