I have this code:
from PySide6.QtCore import QUrl, QTimer
from PySide6.QtWidgets import QApplication, QMainWindow
from PySide6.QtWebEngineWidgets import QWebEngineView
import sys
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("YouTube Viewer")
self.resize(800, 600)
self.webview = QWebEngineView()
self.setCentralWidget(self.webview)
# Load YouTube
self.webview.setUrl(QUrl("https://www.youtube.com/watch?v=sjkrrmBnpGE"))
# Wait for the page to load
self.webview.loadFinished.connect(self.page_loaded)
def page_loaded(self):
# Create a timer for the delay
timer = QTimer()
timer.setSingleShot(True)
# Function to execute after the delay
def click_owner_channel():
# Execute JavaScript to click the owner channel button using XPath
self.webview.page().runJavaScript(
'document.evaluate("/html/body/ytd-app/div[1]/ytd-page-manager/ytd-watch-flexy/div[5]/div[1]/div/div[2]/ytd-watch-metadata/div/div[2]/div[1]/ytd-video-owner-renderer/div[1]/ytd-channel-name/div/div/yt-formatted-string/a", ' +
'document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click();'
)
pixmap = self.webview.grab()
pixmap.save("screenshot.png")
# Close the window after clicking
timer.singleShot(5000, self.close)
# Start the timer for the delay
timer.singleShot(10000, click_owner_channel)
def main():
# Create a PySide6 application
app = QApplication([])
# Create and show the main window
main_window = MainWindow()
main_window.show()
# Run the application event loop
sys.exit(app.exec())
if __name__ == "__main__":
main()
This is working perfectly fine, but as soon as i try to hide the window using main_window.hide() or commenting out the main_window.show() the script isn't working anymore. I also get a blank / white screenshot. Am I wasting my time with this? Is there really a solution to this? Thanks in advance!
I tried: main_window.hide() commenting out the main_window.show()