Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- # Form implementation generated from reading ui file '/home/imron/Documents/python/iris - web automation/simple/mainwindow.ui'
- #
- # Created by: PyQt5 UI code generator 5.14.1
- #
- # WARNING! All changes made in this file will be lost!
- import os
- from PyQt5 import QtCore, QtGui, QtWidgets
- from selenium import webdriver
- from selenium.webdriver.chrome.options import Options
- from selenium.webdriver.common.keys import Keys
- ADDRESS = '127.0.0.1'
- PORT = '5558'
- DEBUG_ADDRESS = ADDRESS + ':' + PORT
- os.environ['QTWEBENGINE_REMOTE_DEBUGGING'] = PORT
- class Ui_MainWindow(object):
- def setupUi(self, MainWindow):
- MainWindow.setObjectName("MainWindow")
- MainWindow.resize(617, 395)
- self.centralWidget = QtWidgets.QWidget(MainWindow)
- self.centralWidget.setObjectName("centralWidget")
- self.gridLayout = QtWidgets.QGridLayout(self.centralWidget)
- self.gridLayout.setObjectName("gridLayout")
- self.webEngineView = QtWebEngineWidgets.QWebEngineView(self.centralWidget)
- self.webEngineView.setUrl(QtCore.QUrl("about:blank"))
- self.webEngineView.setObjectName("webEngineView")
- self.gridLayout.addWidget(self.webEngineView, 0, 0, 1, 1)
- self.verticalLayout = QtWidgets.QVBoxLayout()
- self.verticalLayout.setObjectName("verticalLayout")
- spacerItem = QtWidgets.QSpacerItem(20, 340, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
- self.verticalLayout.addItem(spacerItem)
- self.pushButton = QtWidgets.QPushButton(self.centralWidget)
- self.pushButton.setMaximumSize(QtCore.QSize(100, 16777215))
- self.pushButton.setObjectName("pushButton")
- self.verticalLayout.addWidget(self.pushButton)
- self.gridLayout.addLayout(self.verticalLayout, 0, 1, 1, 1)
- MainWindow.setCentralWidget(self.centralWidget)
- self.retranslateUi(MainWindow)
- QtCore.QMetaObject.connectSlotsByName(MainWindow)
- # load HTML
- HTML = '<label>Please input some text</label><input type="text" name="q">'
- self.webEngineView.setHtml(HTML)
- #signal n slot
- self.pushButton.clicked.connect(self.pushButton_onClicked)
- # webdriver
- self.bot = None
- def retranslateUi(self, MainWindow):
- _translate = QtCore.QCoreApplication.translate
- MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
- self.pushButton.setText(_translate("MainWindow", "Fill Edit"))
- def driverResult(self, bot):
- if bot:
- self.bot = bot
- # execute
- self.executor = EXECUTOR(bot)
- self.executor.start()
- def pushButton_onClicked(self):
- if not self.bot:
- self.driver = WEBDRIVERBOT()
- self.driver.bot.connect(self.driverResult)
- self.driver.start()
- else:
- self.driverResult(self.bot)
- class WEBDRIVERBOT(QtCore.QThread):
- bot = QtCore.pyqtSignal(object)
- def __init__(self):
- QtCore.QThread.__init__(self)
- def run(self):
- #setup browser
- options = Options()
- options.add_experimental_option("debuggerAddress", DEBUG_ADDRESS)
- desired_capabilities=options.to_capabilities()
- # connect browser
- try:
- driver = webdriver.Chrome(options=options, desired_capabilities=desired_capabilities)
- self.bot.emit(driver)
- print(driver)
- except Exception as error:
- print(error)
- self.bot.emit(None)
- class EXECUTOR(QtCore.QThread):
- msg = QtCore.pyqtSignal(object)
- def __init__(self, bot):
- QtCore.QThread.__init__(self)
- self.bot = bot
- def run(self):
- try:
- self.elem = self.bot.find_element_by_name('q')
- self.elem.send_keys("w")
- except Exception as error:
- print('Error:', error)
- self.msg.emit(None)
- from PyQt5 import QtWebEngineWidgets
- if __name__ == "__main__":
- import sys
- app = QtWidgets.QApplication(sys.argv)
- app.setApplicationDisplayName('Chrome')
- app.setApplicationVersion('80.0.3497.128')
- MainWindow = QtWidgets.QMainWindow()
- ui = Ui_MainWindow()
- ui.setupUi(MainWindow)
- MainWindow.show()
- sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement