Quantcast
Channel: QtWebEngine
Viewing all articles
Browse latest Browse all 13965

[[qanda:topic_unsolved]] Segmentation Fault, PyQt5 Webdriver send_keys

$
0
0

Spent couple hours to try how to solve this problem, Using embeded QtWebEngine, instead of external browser, but when try to send_keys("some word") return "segmentation fault"

but its OK when send_keys without any string(send_keys("")), but its useless action

all webdriver action already under QThread

#!/usr/bin/python


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):
    bot = 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')
                print('after', 'find_element_by_name')

                # segmentation fault when excute this line
                self.elem.send_keys("hallo")
            except Exception as error:
                print(error)
                self.bot.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_())

if you try code above, save as Chrome(without ".py") and make it executable


Viewing all articles
Browse latest Browse all 13965

Trending Articles