I'm working on automating a couple of websites altogether in a PyQt Program, Getting issues with PyAutoGUI, I've got 4 webengineviews side by side. Now I want to automate typing in and pressing enter on all 4 of them with just one click.
To implement that I have used Pyautogui, but the problem is 2/4 websites have a button on them to press enter and pressing enter presses a /n on the website.
To tackle that, made different functions for each of those websites and added PyAutoGUI click by image recognition thing. But for some reason it works on Webengineview1, 2, 4. Not on webengineview 3.
I've individually tried on webengineview 3, and it worked. I think it is a logic/timer issue. I have not yet been able to tackle it. Here is my Code: If you're wondering why I have a try and except block for WebengineView 3 it is because the button to press enter changes after the button is pressed once.
And if you're wondering why I am clicking on ak.png, it is the typebox for webengineview no.4 which needs to be clicked on once to be initialised. Same thing is with the typebox/textbox on webengineview no.3 where we have typebxcl.png.
P.S. I'm sorry for the cryptic logic. :(
import sys
import time
import pyautogui
from PySide6 import QtWidgets
from PySide6.QtWebEngineWidgets import QWebEngineView
from PySide6.QtWebEngineCore import QWebEngineProfile, QWebEnginePage
from PySide6.QtWidgets import QLineEdit
from PySide6.QtCore import QUrl, QTimer
from MainWindow2 import Ui_MainWindow
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super(MainWindow, self).__init__()
# Set up UI
self.setupUi(self)
self.setWindowTitle("4")
# Create a persistent profile
self.profile = QWebEngineProfile("myProfile", self)
# Find widgets
self.lineEdit = self.findChild(QLineEdit, "lineEdit")
self.webEngineView1 = self.findChild(QWebEngineView, "webEngineView")
self.webEngineView2 = self.findChild(QWebEngineView, "webEngineView_2")
self.webEngineView3 = self.findChild(QWebEngineView, "webEngineView_3")
self.webEngineView4 = self.findChild(QWebEngineView, "webEngineView_4")
# Set the profile for each webEngineView
self.webEngineView1.setPage(QWebEnginePage(self.profile, self.webEngineView1))
self.webEngineView2.setPage(QWebEnginePage(self.profile, self.webEngineView2))
self.webEngineView3.setPage(QWebEnginePage(self.profile, self.webEngineView3))
self.webEngineView4.setPage(QWebEnginePage(self.profile, self.webEngineView4))
self.lineEdit.returnPressed.connect(self.type)
def type(self):
input_value = self.lineEdit.text()
QTimer.singleShot(0, lambda: self.setFocusAndType(self.webEngineView1, input_value))
QTimer.singleShot(2000, lambda: self.setFocusAndType24(self.webEngineView2, input_value))
QTimer.singleShot(3750, lambda: self.setFocusAndType2(self.webEngineView3, input_value))
QTimer.singleShot(6000, lambda: self.setFocusAndType1(self.webEngineView4, input_value))
def setFocusAndType(self, webview, input_value):
webview.setFocus()
pyautogui.write(input_value)
pyautogui.click('cpenter.png')
def setFocusAndType1(self, webview, input_value):
webview.setFocus()
pyautogui.click('ak.png')
pyautogui.write(input_value)
pyautogui.press("enter")
def setFocusAndType24(self, webview, input_value):
webview.setFocus()
pyautogui.write(input_value)
pyautogui.press('enter')
def setFocusAndType2(self, webview, input_value):
#webview.setFocus()
#pyautogui.click('center1.png')
#pyautogui.write(input_value)
#pyautogui.click('cle2.png')
webview.setFocus()
pyautogui.click('typebxcl.png')
pyautogui.write(input_value)
def tryt():
try:
pyautogui.click('clenter1.png')
except pyautogui.ImageNotFoundException:
pyautogui.click('cld2.png')
QTimer.singleShot(50, tryt)
#everything goes above this
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.showMaximized()
app.exec()