Hello guys, I wrote a simple browser based on wx.html2.webview to navigate a specific website and able to distinguish what page needs basic authentication management. In practice, the user receives a couple user,password it is stored securely and everywhere the user navigates to a protected page the browser automatically inserts user and password in the http get message. This management works well, what does not work is that if I click on a link inside the web page loaded by my mini-browser it is not sure it fires OnNavigating event. For instance, I have a link to an external web site, in this case if I click on the link the event is not fired, if I write the url on my mini browser combo and then i press enter it works. I supposed also to be able to intercept in OnNavigating every time I click on a link or on a button in a web page also if the link redirects to a non-html file (may be a pdf or a mp3, mp4 ando so on) so that I may open it with the ore appropriate application but also in these cases OnNavigating does not fire. Am I missing something?
Hi,
How about this?
self.Bind(wx.html2.EVT_WEBVIEW_NAVIGATING,
lambda v: print(v.URL)
You can receive the navigation events wx.html2.WebViewEvent
it is what I do. Let me explain better: I did a navigation bar using a ComboBox, it is editable so I can write a url, suppose I write https://google.com and press enter (or the crresponding GO button on the navigation bar) it wors, it fires OnNavigating and OnNavigated events that I manage as you suggested. But if in the web page shown by webview there is a link or a button linked to https://google.com it does not fire OnNavigating and OnNavigates and does not open the linked page same situation if the link points to pdf, mp3 or a file format not directly supported by webview. I supposed at least they fire OnNavigating and/or OnError but they don’t so I would like to understand if it is a bug or a limit and if there is any known workaround or if I need to derive and modify webview because I need this feature.
I see.
I wrote a test code.
In this test, the navigation events come properly when you click the hyperlink to google, pdf, and png.
It works the same way even when it is linked to an unsupported file format such as mp4, and the default action invokes, i.e. downloading. It seems fine and I still cannot figure out what your problem is.
I tested with Python 3.8.6 Windows 64 bit + wxpython 4.1.1
Note that SetPage
doesn’t work and LoadURL
must be used. (I don’t know why)
import os
import wx
import wx.html2
class Frame(wx.Frame):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.browser = wx.html2.WebView.New(self)
self.Bind(wx.html2.EVT_WEBVIEW_NAVIGATING,
lambda v: self.SetTitle(v.URL))
menubar = wx.MenuBar()
menu = wx.Menu()
item = menu.Append(101, 'Go home')
menu.Bind(wx.EVT_MENU, self.GoHome, item)
menubar.Append(menu, 'Navi')
self.SetMenuBar(menubar)
def GoHome(self, evt=None):
## self.browser.SetPage(open("index.html").read(), baseUrl='') # bad
self.browser.LoadURL("file:///" + os.path.abspath("index.html"))
if __name__ == '__main__':
app = wx.App()
frm = Frame(None)
frm.GoHome()
frm.Show()
app.MainLoop()
and the test HTML file (please save as index.html)
<!DOCTYPE html>
<html lang="en">
<body>
<p> Links </p>
<ul>
<li><a href="https://www.google.co.jp/">google</a>
<li><a href="https://discuss.wxpython.org/">wxpython</a>
<li><a href="https://github.com/wxWidgets/Phoenix/raw/master/demo/bitmaps/splash.png">png sample</a>
<li><a href="https://courses.physics.ucsd.edu/2016/Winter/physics2d/einsteinonrelativity.pdf">pdf sample</a>
<li><a href="https://samplelib.com/sample-mp4.html">mp4 sample</a>
<li><a href="sample.pdf">pdf</a>
<li><a href="sample.mp4">mp4</a>
<li><a href="sample.tif">tif</a>
</ul>
</body>
</html>