I am currently learning PyQt6 and I am interested in learning more about the capabilities of QtWebEngine. I would appreciate if you could recommend some guides or tutorials for studying QtWebEngine. Thank you in advance for your help. I understand that the issue lies in the fact that the img tag does not appear on the web page in time for the JavaScript findElementByClassName method to find it.
from PyQt6.QtWidgets import QApplication
from PyQt6.QtWebEngineWidgets import QWebEngineView
from PyQt6.QtCore import QUrl
import sys
def js_handle_callback(result):
print('Result: ', type(result), result)
js_code = """
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
var img = document.getElementsByClassName('lnXdpd');
canvas.height = img.naturalHeight;
canvas.width = img.naturalWidth;
context.drawImage(img, 0, 0, img.naturalWidth, img.naturalHeight);
var base64String = canvas.toDataURL();
base64String;
"""
def js_run():
page.runJavaScript(js_code, js_handle_callback)
if __name__ == '__main__':
app = QApplication(sys.argv)
view = QWebEngineView()
page = view.page()
page.loadFinished.connect(js_run)
view.load(QUrl('https://google.com'))
view.show()
sys.exit(app.exec())
Output:
Result: <class 'NoneType'> None
Tried to use signal loadFinished, but it doesn't help. The JavaScript window.addEventListener('load', function(){ My Code }) also doesn't help because the callback doesn't wait for the 'load' event.