Saya jarang memiliki masalah besar dengan kode saya. Saat ini saya sedang membangun Bot Whatsapp dengan antarmuka. Sekarang ketika saya mulai menjalankan Bot, saya tidak bisa menghentikannya sama sekali. Saya banyak mencari di Google dan tahu saya membutuhkan utas, tetapi semua yang saya coba salah. Tolong bantu saya dengan ini!! Semua yang diblokir dapat diabaikan dan hanya beberapa yang gagal mencoba menyelesaikan masalah ini. Berikut kodenya:

from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import QtGui
import sys, threading, time

class interface(QMainWindow):

    def __init__(self):
        super().__init__()
        self.initMe()


    def initMe(self):
        self.boldFont = QtGui.QFont()
        self.boldFont.setBold(True)
        self.setGeometry(1200, 250, 600, 500)
        self.title = self.setWindowTitle("Whatsapp Bot interface")
        self.Layout_components()
        self.labels()
        self.input()
        self.button()
        self.setFont(QFont("Arial", 15))
        self.setWindowIcon(QIcon("Bot.png"))

        self.show()


    def Layout_components(self):
        self.exit_ = QAction(QIcon("Bot.png"), "Exit", self)
        self.stop_ = QAction(QIcon("Bot.png"), "Stop", self)
        self.start_ = QAction(QIcon("Bot.png"), "Start", self)
        self.exit_.triggered.connect(self.exit)
        self.stop_.triggered.connect(self.stop)
        self.start_.triggered.connect(self.start)

        menubar = self.menuBar()
        file = menubar.addMenu("Bot")
        file.addAction(self.exit_)
        file.addAction(self.stop_)
        file.addAction(self.start_)

    def labels(self):

        labelp1 = QLabel("picture", self)
        labelp1.setPixmap(QPixmap("Bot.png"))
        labelp1.setGeometry(340, 100, 210, 180)

        labelp2 = QLabel("picture1", self)
        labelp2.setPixmap(QPixmap("whatsapp_symbol.png"))
        labelp2.setScaledContents(True)
        labelp2.resize(80, 80)
        labelp2.setGeometry(50, 400, 80, 80)

        label1 = QLabel("description", self)
        label1.setGeometry(50, 40, 500, 50)
        label1.setText("Welcome to my Whatsappbot. Are you ready to fuck other people with endless spam up. Have fun!!!")
        label1.setFont(QFont("Arial", 14))
        label1.setStyleSheet("border : 2px solid black;")
        label1.setWordWrap(True)

        label2 = QLabel("adress", self)
        label2.setGeometry(50, 100, 150, 50)
        label2.setText("adressed to: ")

        label2 = QLabel("message", self)
        label2.setGeometry(50, 300, 150, 50)
        label2.setText("message: ")

        label2 = QLabel("amount", self)
        label2.setGeometry(50, 200, 200, 50)
        label2.setText("amount of messages: ")

        self.label_input = QLabel("counter", self)
        self.label_input.setGeometry(170, 260, 80, 20)
        self.label_input.setFont(QFont("Arial", 15))
        self.label_input.setAlignment(Qt.AlignCenter)
        self.label_input.setStyleSheet("border:2px solid black")


    def input(self):
        self.input_adressed = QLineEdit(self)
        self.input_adressed.setGeometry(50, 150, 150, 30)
        self.input_adressed.textChanged.connect(self.clicked_adressed)

        self.input_message = QLineEdit(self)
        self.input_message.setGeometry(50, 350, 150, 30)
        self.input_message.textChanged.connect(self.clicked_message)

        self.input_amount = QSlider(self)
        self.input_amount.setGeometry(50, 240, 100, 70)
        self.input_amount.setMaximum(1000)
        self.input_amount.valueChanged.connect(self.clicked_amount)

    def button(self):

        self.but_stop = QPushButton("Stop", self)
        self.but_stop.setGeometry(340, 360, 210, 50)
        self.but_stop.setFont(self.boldFont)
        self.but_stop.clicked.connect(self.stop)

        self.but_start = QPushButton("Start", self)
        self.but_start.setGeometry(340, 300, 210, 50)
        self.but_start.setFont(self.boldFont)
        self.but_start.clicked.connect(self.start)

        self.but_exit = QPushButton("Exit", self)
        self.but_exit.setGeometry(340, 420, 210, 50)
        self.but_exit.setFont(self.boldFont)
        self.but_exit.clicked.connect(self.exit)

    def clicked_amount(self):

        self.amount = self.input_amount.value()
        if self.amount >= 0:
            self.label_input.setText(f"{self.amount}")

        return self.amount

    def clicked_adressed(self):

        #print(self.input_adressed.text())
        return self.input_adressed.text()

    def clicked_message(self):

        return self.input_message.text()

    def stop(self):
        print("programm stopps ...")


    def exit(self):
        #print("programm exits ...")
        app.exit()
        sys.exit()

    def start(self):
        pop_up = QMessageBox(self)
        pop_up.setWindowTitle("Start Bot")
        pop_up.setText("Do you really wanna start the spam ?!")
        pop_up.setIcon(QMessageBox.Question)
        pop_up.setStandardButtons(QMessageBox.Cancel | QMessageBox.No | QMessageBox.Yes)
        pop_up.setDefaultButton(QMessageBox.Yes)
        x = pop_up.exec_()

        if x == QMessageBox.Yes:
            print("programm starts ...")
            #self.worker_start()
            self.main()

    # def worker_start(self):
    #         self.worker = WorkerThread()
    #         self.worker.start()
    #         self.worker.finished.connect(self.worker_finished)
    #         self.worker.update_progress.connect(self.update_progress)
    #
    # def worker_finished(self):
    #     QMessageBox.information(self, "Done", "Worker thread complete")


    def main(self):

        message = self.clicked_message()
        addressed = self.clicked_adressed()
        profile_path = "user-data-dir=/home/daniel/.config/google-chrome/storing"

        options = webdriver.ChromeOptions()
        options.add_argument(profile_path)
        browser = webdriver.Chrome(executable_path="./chromedriver", options=options)
        browser.get("https://web.whatsapp.com")

        time.sleep(4)
        search = WebDriverWait(browser, 500).until(expected_conditions.presence_of_element_located(
            (By.CSS_SELECTOR, "#side > div.SgIJV > div > label > div > div._2_1wd.copyable-text.selectable-text")))
        search.send_keys(addressed)
        time.sleep(1)
        search.send_keys(Keys.ENTER)
        send = WebDriverWait(browser, 500).until(expected_conditions.presence_of_element_located((By.CSS_SELECTOR,"#main > footer > div.vR1LG._3wXwX.copyable-area > div._2A8P4._2A1WX > div > div._2_1wd.copyable-text.selectable-text")))

        for i in range(self.clicked_amount()):
            send.send_keys(message)
            send.send_keys(Keys.ENTER)

# class WorkerThread(QThread, interface):
#     update_progress = pyqtSignal(int)
#     def run(self):
#         for x in range(100):
#             print(x)
#             time.sleep(2)
#             self.update_progress.emit(x)

            # self.setEnabled(True)
            # message = self.clicked_message()
            # addressed = self.clicked_adressed()
            # profile_path = "user-data-dir=/home/daniel/.config/google-chrome/storing"
            #
            # options = webdriver.ChromeOptions()
            # options.add_argument(profile_path)
            # browser = webdriver.Chrome(executable_path="./chromedriver", options=options)
            # browser.get("https://web.whatsapp.com")
            #
            # time.sleep(4)
            # search = WebDriverWait(browser, 500).until(expected_conditions.presence_of_element_located(
            #     (By.CSS_SELECTOR, "#side > div.SgIJV > div > label > div > div._2_1wd.copyable-text.selectable-text")))
            # search.send_keys(addressed)
            # time.sleep(1)
            # search.send_keys(Keys.ENTER)
            # send = WebDriverWait(browser, 500).until(expected_conditions.presence_of_element_located((By.CSS_SELECTOR,
            #                                                                                           "#main > footer > div.vR1LG._3wXwX.copyable-area > div._2A8P4._2A1WX > div > div._2_1wd.copyable-text.selectable-text")))
            #
            # for i in range(self.clicked_amount()):
            #     send.send_keys(message)
            #     send.send_keys(Keys.ENTER)


app = QApplication(sys.argv)
x = interface()
sys.exit(app.exec_())
0
user15576343 7 April 2021, 20:37

1 menjawab

Jawaban Terbaik

Logika dalam hal ini adalah untuk mendapatkan informasi dari GUI (jumlah, alamat, dan pesan) dan menjalankan fungsi yang memakan waktu di utas lain yang meneruskan informasi ke sana.

from functools import cached_property
import sys
import threading
import time


from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *


class Worker:
    def start(self, amount, adressed, message):
        threading.Thread(
            target=self._execute,
            args=(
                amount,
                adressed,
                message,
            ),
            daemon=True,
        ).start()

    def _execute(self, amount, adressed, message):
        profile_path = "user-data-dir=/home/daniel/.config/google-chrome/storing"

        options = webdriver.ChromeOptions()
        options.add_argument(profile_path)
        browser = webdriver.Chrome(executable_path="./chromedriver", options=options)
        browser.get("https://web.whatsapp.com")

        time.sleep(4)
        search = WebDriverWait(browser, 500).until(
            expected_conditions.presence_of_element_located(
                (
                    By.CSS_SELECTOR,
                    "#side > div.SgIJV > div > label > div > div._2_1wd.copyable-text.selectable-text",
                )
            )
        )
        search.send_keys(addressed)
        time.sleep(1)
        search.send_keys(Keys.ENTER)
        send = WebDriverWait(browser, 500).until(
            expected_conditions.presence_of_element_located(
                (
                    By.CSS_SELECTOR,
                    "#main > footer > div.vR1LG._3wXwX.copyable-area > div._2A8P4._2A1WX > div > div._2_1wd.copyable-text.selectable-text",
                )
            )
        )

        for i in range(amount):
            send.send_keys(message)
            send.send_keys(Keys.ENTER)


class interface(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initMe()

    def initMe(self):
        self.boldFont = QFont()
        self.boldFont.setBold(True)
        self.setGeometry(1200, 250, 600, 500)
        self.title = self.setWindowTitle("Whatsapp Bot interface")
        self.Layout_components()
        self.labels()
        self.input()
        self.button()
        self.setFont(QFont("Arial", 15))
        self.setWindowIcon(QIcon("Bot.png"))

        self.show()

    def Layout_components(self):
        self.exit_ = QAction(QIcon("Bot.png"), "Exit", self)
        self.stop_ = QAction(QIcon("Bot.png"), "Stop", self)
        self.start_ = QAction(QIcon("Bot.png"), "Start", self)
        self.exit_.triggered.connect(self.exit)
        self.stop_.triggered.connect(self.stop)
        self.start_.triggered.connect(self.start)

        menubar = self.menuBar()
        file = menubar.addMenu("Bot")
        file.addAction(self.exit_)
        file.addAction(self.stop_)
        file.addAction(self.start_)

    def labels(self):

        labelp1 = QLabel("picture", self)
        labelp1.setPixmap(QPixmap("Bot.png"))
        labelp1.setGeometry(340, 100, 210, 180)

        labelp2 = QLabel("picture1", self)
        labelp2.setPixmap(QPixmap("whatsapp_symbol.png"))
        labelp2.setScaledContents(True)
        labelp2.resize(80, 80)
        labelp2.setGeometry(50, 400, 80, 80)

        label1 = QLabel("description", self)
        label1.setGeometry(50, 40, 500, 50)
        label1.setText(
            "Welcome to my Whatsappbot. Are you ready to fuck other people with endless spam up. Have fun!!!"
        )
        label1.setFont(QFont("Arial", 14))
        label1.setStyleSheet("border : 2px solid black;")
        label1.setWordWrap(True)

        label2 = QLabel("adress", self)
        label2.setGeometry(50, 100, 150, 50)
        label2.setText("adressed to: ")

        label2 = QLabel("message", self)
        label2.setGeometry(50, 300, 150, 50)
        label2.setText("message: ")

        label2 = QLabel("amount", self)
        label2.setGeometry(50, 200, 200, 50)
        label2.setText("amount of messages: ")

        self.label_input = QLabel("counter", self)
        self.label_input.setGeometry(170, 260, 80, 20)
        self.label_input.setFont(QFont("Arial", 15))
        self.label_input.setAlignment(Qt.AlignCenter)
        self.label_input.setStyleSheet("border:2px solid black")

    def input(self):
        self.input_adressed = QLineEdit(self)
        self.input_adressed.setGeometry(50, 150, 150, 30)

        self.input_message = QLineEdit(self)
        self.input_message.setGeometry(50, 350, 150, 30)

        self.input_amount = QSlider(self)
        self.input_amount.setGeometry(50, 240, 100, 70)
        self.input_amount.setMaximum(1000)

        self.input_amount.valueChanged.connect(self.label_input.setNum)

    def button(self):
        self.but_stop = QPushButton("Stop", self)
        self.but_stop.setGeometry(340, 360, 210, 50)
        self.but_stop.setFont(self.boldFont)
        self.but_stop.clicked.connect(self.stop)

        self.but_start = QPushButton("Start", self)
        self.but_start.setGeometry(340, 300, 210, 50)
        self.but_start.setFont(self.boldFont)
        self.but_start.clicked.connect(self.start)

        self.but_exit = QPushButton("Exit", self)
        self.but_exit.setGeometry(340, 420, 210, 50)
        self.but_exit.setFont(self.boldFont)
        self.but_exit.clicked.connect(self.exit)

    @cached_property
    def worker(self):
        return Worker()

    @property
    def amount(self):
        value = self.input_amount.value()
        return value

    @property
    def adressed(self):
        return self.input_adressed.text()

    @property
    def message(self):
        return self.input_message.text()

    def stop(self):
        print("programm stopps ...")

    def exit(self):
        # print("programm exits ...")
        QCoreApplication.quit()

    def start(self):
        pop_up = QMessageBox(self)
        pop_up.setWindowTitle("Start Bot")
        pop_up.setText("Do you really wanna start the spam ?!")
        pop_up.setIcon(QMessageBox.Question)
        pop_up.setStandardButtons(QMessageBox.Cancel | QMessageBox.No | QMessageBox.Yes)
        pop_up.setDefaultButton(QMessageBox.Yes)
        if pop_up.exec_() == QMessageBox.Yes:
            print("programm starts ...")
            self.worker.start(self.amount, self.adressed, self.message)


app = QApplication(sys.argv)
x = interface()
sys.exit(app.exec_())
2
eyllanesc 7 April 2021, 18:17