Потратил пару часов, чтобы попытаться решить эту проблему, используя встроенный QtWebEngine вместо внешнего браузера, но при попытке send_keys ("some word") вернуть "ошибка сегментации"
, но это нормально, когда send_keys без какой-либо строки (send_keys ("")), но ее бесполезное действие
#!/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_())
- это вывод из консоли
DevTools listening on ws://127.0.0.1:5558/devtools/browser/ec8376c9-a7a0-4232-a579-5ec3b2377393
Remote debugging server started successfully. Try pointing a Chromium-based browser to http://127.0.0.1:5558
<selenium.webdriver.chrome.webdriver.WebDriver (session="7b32fa54362e3053b100db0895a856c1")>
find_element_by_name
Segmentation fault (core dumped)
FYI
if Вы пытаетесь код выше, сохранить как Chrome (без ".py") и сделать его исполняемым