I am trying to understand how this works and am struggling to figure out how to use it. The only examples I can find are not in Python and apparently I'm not that good at translating.
I've been digging through the help() results for most of these modules but am still not able to figure out how they work. Now if I read it right this should be able to be used to ignore a certificate error when loading a page.
QWebEngineCertificateError.ignoreCertificateError()
But when I try to run this I get the following error. I am pretty sure I am using it wrong but I can't find a good example of how its supposed to work.
TypeError: ignoreCertificateError(self): first argument of unbound method must have type 'QWebEngineCertificateError'
In a normal browser when you run into a cert error like this "ERR_CERT_AUTHORITY_INVALID" you can choose to proceed anyway. That option doesn't seem to be a default feature of QWebEngineView. What I am trying to do is implement that or to have it automatically just ignore the error and proceed.
Does anyone out there understand how to do this and are you willing to give me a pointer in the right direction? I'm stumped. I tried to get around the problem by just embedding a Chrome or edge browser into the app but it won't let my type in the browser though I can click on things and right click.
Here is an example code opening a website that triggers the same error. Its not what I need to load but its a site that triggers the same error.
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication
import sys
app = QApplication(sys.argv)
webview = QWebEngineView()
webview.load(QUrl("https://www.us.army.mil/"))
webview.show()
sys.exit(app.exec_())
screenshot of the error:
You have to override the certificateError() method of QWebEnginePage:
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineView
class WebEnginePage(QWebEnginePage):
def certificateError(self, error):
# If you want to ignore the certificates of certain pages
# then do something like
# if error.url() == QUrl("https://www.us.army.mil/"):
# error.ignoreCertificateError()
# return True
# return super().certificateError(error)
error.ignoreCertificateError()
return True
def main(args):
app = QApplication(args)
webview = QWebEngineView()
page = WebEnginePage()
webview.setPage(page)
webview.load(QUrl("https://www.us.army.mil/"))
webview.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main(sys.argv)
```
Related
I'm trying to use the spotify web player on a python browser windows.
I try it in two ways:
import webview
webview.create_window('spotify', 'https://open.spotify.com')
webview.start()
import sys
import time
from PyQt5.Qt import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
web = QWebEngineView()
web.load(QUrl("https://open.spotify.com/"))
web.showFullScreen()
sys.exit(app.exec_())
After I log in with my account I get:
Enable secure playback in your browser.
Is there way I can do this? In the spotify support page it tells me to enable protected content playback or play drm. I am not sure if it is possible.
By #metatoaster 's suggestion, I tried to set the QTWEBENGINE_CHROMIUM_FLAGS environment variable to --widevine-path="/home/<myusername>/.mozilla/firefox/237oaqbb.default-release/gmp-widevinecdm/4.10.2449.0/libwidevinecdm.so"
After I got the location from locate libwidevinecdm.so, and it worked!
I tried with the spotify's path for the library but that didn't work.
I've been having an issue migrating to PyQt6 with QAudioOutput and QMediaPlayer where the QMediaPlayer object seems to not work with any QAudioOutput I make. If I set a QAudioOutput object the video will fail to render and the event loop gets sluggish like buggy things are happening. Also the QMediaPlayer does not seem to be incrementing the QAudioOutput object's reference counter when QMediaPlayer.setAudioOutput is used, because unless I keep a reference to the object myself it gets cleared.
Here is some demo code:
import sys
from PyQt6.QtWidgets import QMainWindow, QApplication
from PyQt6.QtCore import QUrl
from PyQt6.QtMultimedia import QMediaPlayer, QAudioOutput
from PyQt6.QtMultimediaWidgets import QVideoWidget
class MainWin(QMainWindow):
def __init__(self, file_path):
super(MainWin, self).__init__()
self.cent_wid = QVideoWidget()
self.setCentralWidget(self.cent_wid)
self.player = QMediaPlayer()
self.audio_output = QAudioOutput()
#self.player.setAudioOutput(self.audio_output)
self.audio_output.setVolume(1.0)
self.player.setVideoOutput(self.cent_wid)
self.file_path = file_path
def showEvent(self, a0) -> None:
super(MainWin, self).showEvent(a0)
self.player.setSource(QUrl.fromLocalFile(self.file_path))
self.player.play()
if __name__ == '__main__':
app = QApplication([])
frm = MainWin(sys.argv[1])
frm.show()
app.exec()
For me, the above will run and play the video file (first argument for path), but the "player.setAudioOutput" is commented out. If it is uncommented then the player will fail. I've tried manually setting the QAudioDevice and PyQt (6.2.3, 6.2.2). Despite messing around for quite a while I can't get anything to work. Any ideas?
Although not a solution to this problem I have determined that the issue is with the vorbis audio codec on windows. Since Qt dropped DirectShow and only supports WMF this caused an issue on my computer. Unfortunately, I have not been able to get Qt to cooperate with any attempts to install codecs. Not 3rd party codecs or the "Web Media Extensions" from ms store. Below is some code that appears to prove that the vorbis codec is the issue (along with only files needing that codec breaking Qt):
from PyQt6.QtMultimedia import QMediaFormat
mf = QMediaFormat()
for codec in mf.supportedAudioCodecs(QMediaFormat.ConversionMode.Decode):
print(codec.name)
I'm having a bizarre issue where this simple QWebEngine code runs perfectly fine as a regular user on Windows 10 (loads the page fully), but when I elevate my own default user account it stops loading the page. The progress output will go from 0->100 and not call loadFinished on the browser or show any output on the page. I've tried running it as an elevated standard Administrator built-in account and it seemed to work oddly enough.
Here's the unloaded page:
and here's it working properly:
Running with os.environ["QT_DEBUG_PLUGINS"] = "1" doesn't show any discrepancies between the elevated and non-elevated processes.
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication, QMainWindow
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.browser = QWebEngineView()
self.browser.setUrl(QUrl('https://www.google.com'))
self.browser.loadProgress.connect(self.on_load_progress)
self.setCentralWidget(self.browser)
self.show()
def on_load_progress(self, progress: int):
print(f'loading progress:[{progress}]...')
app = QApplication(sys.argv)
main_win = MainWindow()
app.exec()
After trying many things and going through many logs, the only partial solution I've found to this problem is adding --no-sandbox to the QApplication argument list to make the Chromium webdriver work at all. For my app this is enough but running without sandboxing is less than ideal for a broader solution.
For those interested I found the answer thanks to this thread (has a lot more relevant info than my descriptions).
I am building a simple web app called from Python. I am using the below code. What is the easiest way to programatically grant access to the Cam & Mic when this page is loaded? I have only found C++ examples on the web and cannot find a way to do this within Python code.
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import QUrl
app = QApplication([])
view = QWebEngineView()
view.load(QUrl("https://test.webrtc.org/"))
view.show()
app.exec_()
To give permission you must use the setFeaturePermission method of QWebEnginePage, but you must do it when the view asks you to do so when it emits the featurePermissionRequested signal, this will indicate the url and the feature.
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage
from PyQt5.QtCore import QUrl
class WebEnginePage(QWebEnginePage):
def __init__(self, *args, **kwargs):
QWebEnginePage.__init__(self, *args, **kwargs)
self.featurePermissionRequested.connect(self.onFeaturePermissionRequested)
def onFeaturePermissionRequested(self, url, feature):
if feature in (QWebEnginePage.MediaAudioCapture,
QWebEnginePage.MediaVideoCapture,
QWebEnginePage.MediaAudioVideoCapture):
self.setFeaturePermission(url, feature, QWebEnginePage.PermissionGrantedByUser)
else:
self.setFeaturePermission(url, feature, QWebEnginePage.PermissionDeniedByUser)
app = QApplication([])
view = QWebEngineView()
page = WebEnginePage()
view.setPage(page)
view.load(QUrl("https://test.webrtc.org/"))
view.show()
app.exec_()
So I found out that PyQt on the Raspberry Pi does not include support for the WebEngine capabilities. Therefore the WebEngineView class in PyQt cannot be used on the Pi. (I dont really understand why it works fine on Ubuntu but not on Raspbian, but anyway...).
I started down the path of using Qt itself, but then learned you can use the following approach
os.system('chromium-browser --use-fake-ui-for-media-stream %s' % URL)
to start Chrome with access to the microphone and camera pre-granted.
I'm starting to play with QWebView in PySide.
I can load HTML into the web view and view it, however I can't seem to load online pages or set urls. Here is the example I've got:
import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtWebKit import *
app = QApplication(sys.argv)
web = QWebView()
web.setUrl(QUrl("https://www.google.com"))
web.show()
sys.exit(app.exec_())
All I get from this is a nice blank white window. Anyone know what's missing?
Maybe you are behind a proxy.
I've experienced the same behavior and setting a Proxy for the Application helped.
from PySide.QtNetwork import QNetworkProxy
proxy = QNetworkProxy()
proxy.setType(QNetworkProxy.Socks5Proxy);
proxy.setType(QNetworkProxy.HttpProxy);
proxy.setHostName("*********");
proxy.setPort(****);
QNetworkProxy.setApplicationProxy(proxy);