Interface for python
I have an instagram bot written in selenium
. In Qt Designer I made an interface for it, but I can't connect it in any way.
How do I get the values from the interface elements by the authorization function type into the bot variables?
The file contains two classes: one class is a bot, the second class is an interface to PyQt5.
# username = input('Введите ваш логин: ')
# password = input('Введите ваш пороль: ')
class InstagramBot():
username = pyqtsignal(str)
password = pyqtsignal(str)
def __init__(self,username,password):
self.username = username
self.password = password
# options = Options()
# options.add_argument("--headless")
self.browser = webdriver.Firefox()
def close_browser(self):
self.browser.close()
self.browser.quit()
def login(self):
browser = self.browser
browser.get('https://www.instagram.com/')
time.sleep(random.randrange(4 ,6))
username_input = browser.find_element_by_name("username")
username_input.clear()
username_input.send_keys(username)
time.sleep(5)
password_input = browser.find_element_by_name("password")
password_input.clear()
password_input.send_keys(password)
password_input =
browser.find_element_by_xpath("/html/body/div[1]/section/main
/article/div[2]/div[1]/div/form/div/div[3]/button/div").click()
time.sleep(10)
class GUI(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QTabWidget.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.login)
self.bot = InstagramBot()
def login(self):
InstagramBot.login()
username = self.ui.lineEdit.text()
password = self.ui.lineEdit_2.text()
self.ui.lineEdit.clear()
self.ui.lineEdit_2.clear()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
mywin = GUI()
mywin.show()
sys.exit(app.exec_())
0
1 answers
Threw example of the work selenium and pyqt5 using the stackoverflow search example.
The main thing:
-
Selenium
is started in a separate thread and communication with it is made via the signalsabout_search_result
andabout_change_title
- Keep in mind that the code in the thread is executed only in the
run
method, and while therun
method is running, the thread will live - The widget is connected to the stream in:
self.bot_thread.about_search_result.connect(self.on_search_result)
andself.bot_thread.about_change_title.connect(self.setWindowTitle)
- To hide selenium, you need to uncomment the line
options.add_argument('--headless')
Code:
from urllib.parse import urljoin
import time
from PyQt5.QtWidgets import (
QApplication, QMainWindow, QWidget, QVBoxLayout, QLineEdit, QHBoxLayout, QPushButton, QTextBrowser
)
from PyQt5.QtCore import QThread, pyqtSignal
# pip install selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options
URL = 'https://ru.stackoverflow.com'
class StackOverFlowBotThread(QThread):
about_search_result = pyqtSignal(str, str)
about_change_title = pyqtSignal(str)
def __init__(self):
super().__init__()
self.is_running = False
options = Options()
# options.add_argument('--headless')
self.driver = webdriver.Firefox(options=options)
self.driver.implicitly_wait(10)
self._search = None
def search(self, text: str):
self._search = text
def run(self):
self.is_running = True
self.driver.get(URL)
print(f'Title: "{self.driver.title}"')
self.about_change_title.emit(self.driver.title)
try:
# Чтобы поток не завершился
while self.is_running:
if self._search:
search_el = self.driver.find_element_by_css_selector('#search .s-input__search')
search_el.clear()
search_el.send_keys(self._search + Keys.RETURN)
# Даем время прогрузиться результату
time.sleep(1)
self.about_change_title.emit(self.driver.title)
for result_el in self.driver.find_elements_by_css_selector('.result-link a[href]'):
title = result_el.text.strip()
url = urljoin(URL, result_el.get_attribute('href'))
self.about_search_result.emit(title, url)
# Поиск уже выполнен
self._search = None
time.sleep(0.05)
finally:
self.driver.quit()
def quit(self):
self.driver.quit()
self.is_running = False
super().quit()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.search = QLineEdit('gil9red')
self.search.returnPressed.connect(self.on_search)
self.button_search = QPushButton('Search')
self.button_search.clicked.connect(self.on_search)
self.result = QTextBrowser()
self.result.setOpenExternalLinks(True)
self.bot_thread = StackOverFlowBotThread()
self.bot_thread.about_search_result.connect(self.on_search_result)
self.bot_thread.about_change_title.connect(self.setWindowTitle)
self.bot_thread.start()
layout_search = QHBoxLayout()
layout_search.addWidget(self.search)
layout_search.addWidget(self.button_search)
main_layout = QVBoxLayout()
main_layout.addLayout(layout_search)
main_layout.addWidget(self.result)
central_widget = QWidget()
central_widget.setLayout(main_layout)
self.setCentralWidget(central_widget)
def on_search(self):
self.result.clear()
text = self.search.text()
self.bot_thread.search(text)
def on_search_result(self, title, url):
self.result.append(f'<a href="{url}">{title}</a>')
def closeEvent(self, event):
self.bot_thread.quit()
if __name__ == '__main__':
app = QApplication([])
mw = MainWindow()
mw.resize(800, 600)
mw.show()
app.exec()
The result of the work:
6
Author: gil9red, 2021-01-21 09:36:05