SeleniumHQ / selenium

A browser automation framework and ecosystem.
https://selenium.dev
Apache License 2.0
30.63k stars 8.18k forks source link

[🐛 Bug]: Segmentation fault when trying to stop a driver in qt #12437

Closed iyamk closed 1 year ago

iyamk commented 1 year ago

What happened?

I made a program but it causes constant segmentation faults when I stop it with the stop button! Help

How can we reproduce the issue?

app.py:

import sys, time, io
from PyQt5 import uic
from PyQt5.QtWidgets import QMainWindow, QApplication, QDialog
from PyQt5.QtGui import QTextCursor
import ui_main, ui_dialog
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.chrome.service import Service as ChromiumService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.os_manager import ChromeType
from selenium_stealth import stealth
import threading
from datetime import datetime

thread_main = None
thread_main_stop = False
ui = None
text_log = None

def log_add(text):
    cursor = QTextCursor(text_log.document())
    cursor.setPosition(len(text_log.toPlainText()))
    text_log.setTextCursor(cursor)
    d = datetime.today()
    text_log.insertPlainText('[' + d.strftime('%H:%M:%S') + '] ' + f'{text}\n')

def start_chrome():
    global thread_main_stop
    op = webdriver.ChromeOptions()
    #op.add_argument('--headless'); op.add_argument('--disable-gpu')
    op.add_argument('start-maximized')
    op.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36')
    op.add_experimental_option("excludeSwitches", ["enable-automation"])
    op.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(
        service=ChromiumService(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()),
        options=op
    )
    stealth(driver,
        languages=['en-US', 'en'],
        vendor='Google Inc.',
        platform='Win32',
        webgl_vendor='Intel Inc.',
        renderer='Intel Iris OpenGL Engine',
        fix_hairline=True,
    )

    driver.get('https://ifconfig.me')

    elem = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'strong[id="ip_address"]')))
    print(elem.text)
    if elem.text != '55.22.44.55':
        dialog = QDialog()
        dialog_ui = ui_dialog.Ui_Dialog()
        dialog_ui.setupUi(dialog)
        dialog.show()

    while True:
        time.sleep(1)
        if thread_main_stop:
            break
    driver.quit()

def click():
    global thread_main, thread_main_stop
    thread_main_stop = False
    thread_main = threading.Thread(target=start_chrome)
    thread_main.start()
    log_add('Start')
    ui.pushButton.setEnabled(False)
    ui.pushButton_2.setEnabled(True)

def click2():
    global thread_main_stop
    thread_main_stop = True
    ui.pushButton.setEnabled(True)
    ui.pushButton_2.setEnabled(False)
    log_add('Stop')

if __name__ == '__main__':
    app = QApplication(sys.argv)

    MainWindow = QMainWindow()
    ui = ui_main.Ui_MainWindow()
    ui.setupUi(MainWindow)
    ui.pushButton.clicked.connect(click)
    ui.pushButton_2.clicked.connect(click2)
    text_log = ui.text_log
    MainWindow.show()

    sys.exit(app.exec_())

ui_dialog.py:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'dialog.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(400, 300)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))

ui_main.py:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'main.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.setWindowModality(QtCore.Qt.NonModal)
        MainWindow.setEnabled(True)
        MainWindow.resize(400, 200)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
        MainWindow.setSizePolicy(sizePolicy)
        MainWindow.setMinimumSize(QtCore.QSize(400, 200))
        MainWindow.setMaximumSize(QtCore.QSize(400, 200))
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(80, 140, 101, 41))
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setEnabled(False)
        self.pushButton_2.setGeometry(QtCore.QRect(220, 140, 111, 41))
        self.pushButton_2.setObjectName("pushButton_2")
        self.text_log = QtWidgets.QTextEdit(self.centralwidget)
        self.text_log.setGeometry(QtCore.QRect(20, 20, 361, 111))
        self.text_log.setReadOnly(True)
        self.text_log.setObjectName("text_log")
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "Start"))
        self.pushButton_2.setText(_translate("MainWindow", "Stop"))

requirements.txt:

selenium==4.8.2
selenium-stealth==1.0.6
webdriver-manager==4.0.0
PyQt5==5.15.9
pyinstaller==5.8.0

### Relevant log output

```shell
(venv) [user@linux test]$ python app.py 
51.158.165.233
Segmentation fault (core dumped)

Run the program and click Start! After immediately Stop and there will be an error. Sometimes an error occurs even if you wait a while and click Stop and I don’t understand what is happening, why segmentation errors! I have the latest version of arch linux

Operating System

arch linux

Selenium version

selenium==4.8.2

What are the browser(s) and version(s) where you see this issue?

all

What are the browser driver(s) and version(s) where you see this issue?

all

Are you using Selenium Grid?

No response

github-actions[bot] commented 1 year ago

@iyamk, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

iyamk commented 1 year ago

Solved by switching to PySimpleGUI! The problem is gone. QT is a flat thing

github-actions[bot] commented 10 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.