I've been working some web automation projects. In this exampe, I reduced my code to basically opening some URLs. I want to add some user friendly interface while the main process was continuing. However when I want to show user to loading gif, It just freeze, because of the selenium process. If selenium process done, then the gif rest time of gif continues. For example, my loading gif is set to 20 secs. The selenium process took 5 secs. So for 5 secs, my loading gif freezes, then the selenium process done and closed, the loading gif animation continues for 14 secs.
Also is there a way to do it with overlay. I searched some examples and tried some, but it didnt work
import sys
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class LoadingScreen(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.setFixedSize(200,200)
self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.CustomizeWindowHint)
self.label_animation = QLabel(self)
self.movie = QMovie("loading.gif")
self.label_animation.setMovie(self.movie)
timer = QTimer(self)
self.startAnimation()
timer.singleShot(20000, self.stopAnimation)
self.show()
def startAnimation(self):
self.movie.start()
def stopAnimation(self):
self.movie.stop()
self.close()
class demo(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Loading Overlay with Selenium Problem")
self.resize(500, 500)
self.center()
self.twitter_icon = QtWidgets.QLabel("")
self.twitter_icon.setAlignment(Qt.AlignCenter)
self.pixmap = QtGui.QPixmap("twitter.png")
self.pixmap = self.pixmap.scaled(64, 64, Qt.KeepAspectRatio, Qt.FastTransformation)
self.twitter_icon.setPixmap(self.pixmap)
self.twt_btn = QtWidgets.QPushButton("Twitter")
v_box = QtWidgets.QVBoxLayout()
v_box.addStretch()
v_box.addWidget(self.twitter_icon)
v_box.addWidget(self.twt_btn)
v_box.addStretch()
self.setLayout(v_box)
self.twt_btn.clicked.connect(self.clkdBtn)
self.show()
def clkdBtn(self):
self.hide()
self.loading = LoadingScreen()
browser = webdriver.Chrome()
browser.get("https://twitter.com/login")
time.sleep(1)
#do more stuff in project instead i add more url
browser.get("https://twitter.com/explore")
time.sleep(1)
browser.get("https://twitter.com/login")
time.sleep(1)
browser.close()
time.sleep(1)
self.show()
def center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
app = QApplication(sys.argv)
dm = demo()
app.exit((app.exec_()))