DOM Aware Browser Python GUI Widget - python

I'm looking for a python browser widget (along the lines of pyQT4's QTextBrowser class or wxpython's HTML module) that has events for interaction with the DOM. For example, if I highlight an h1 node, the widget class should have a method that notifies me something was highlighted and what dom properties that node had (<h1>, contents of the tag, sibling and parent tags, etc). Ideally the widget module/class would give access to the DOM tree object itself so I can traverse it, modify it, and re-render the new tree.
Does something like this exist? I've tried looking but I'm unfortunately not able to find it. Thanks in advance!

It may not be ideal for your purposes, but you might want to take a look at the Python bindings to KHTML that are part of PyKDE. One place to start looking is the KHTMLPart class:
http://api.kde.org/pykde-4.2-api/khtml/KHTMLPart.html
Since the API for this class is based on the signals and slots paradigm used in Qt, you will need to connect various signals to slots in your own code to find out when parts of a document have been changed. There's also a DOM API, so it should also be possible to access DOM nodes for selected parts of the document.
More information can be found here:
http://api.kde.org/pykde-4.2-api/khtml/index.html

I would also love such a thing. I suspect one with Python bindings does not exist, but would be really happy to be wrong about this.
One option I recently looked at (but never tried) is the Webkit browser. Now this has some bindings for Python, and built against different toolkits (I use GTK). However there are available API for the entire Javascript machine for C++, but no Python bindings and I don't see any reason why these can't be bound for Python. It's a fairly huge task, I know, but it would be a universally useful project, so maybe worth the investment.

If you don't mind being limited to Windows, you can use the IE browser control. From wxPython, it's in wx.lib.iewin.IEHtmlWindow (there's a demo in the wxPython demo). This gives you full access to the DOM and ability to sink events, e.g.
ie.document.body.innerHTML = u"<p>Hello, world</p>"

Related

Can I use html&css in kivy and python3?

I am currently developing an embedded multi-touch kivy program with python 3.52 in linux. Kivy is good, but I find GUI developing is quite difficult in some way, and animations are often quite laggy.
Also, I found that the program gets quite slow if I put many widgets in a single page. My program contains a lot of widgets so I am also thinking implementing a webview could help.
So I am looking for html and css views in particular screens for better look and feel (and maybe improvement of animation by using transition?).
I've found Cefpython, but it says it works only for python 2.7 and says it is not stable. And it seemed like it is just getting url from the internet not bringing html and css from a file system. (Correct me if I am wrong.)
Actually, I don't even need a function as a browser, and all I need is html & css for UI/UX development for couple of screens.
My embedded system doesn't always support internet(meaning it should work without internet). So I can't put any url in it. Once again, I am only trying to use them for styling. Is there a good way of doing it? Or there is no such thing that I want?
Or is there any other recommendation?
No, unless you are willing to implement html and css parsers and a web renderers, of course. I would say it's better to find why you find it difficult to do certain things with kivy, and how to remedy that. I've yet to find an UI/layout/template that is easier to do with the web technologies than with kivy, but i'm way more profficient at kivy than at web front end, so i might be biased.

Is there a workaround to avoid iframes in selenium testing?

I am testing complex and non-public webpages with python-selenium, which have interconnected iframes.
To proper click on a button or select some given element in a different iframe I have to switch to that iframe . Now, as contents of the pages might reload to the correct iframe I constantly have to check if the correct iframe is loaded yet, otherwise I have to go back to the default content, do the check again etc.
I find this completely annoying and user-unfriendly behavior of selenium.
Is there a basic workaround to find e.g. an element in ANY iframe? Because I do not care about iframes. I care about elements...
Unfortunately, no, there’s no way around this.
For context, this is likely not simply a limitation of Selenium alone, but of the WebDriver specification and, ultimately, modern browsers. Selenium merely implements the WebDriver specification, which in turn is limited by the features exposed by modern browsers. And browsers likely have good reasons for preventing you from doing this.
What you think of as a single page is actually comprised of multiple documents:
the root document, whose URL and title you see in your browser chrome, and
one or more embedded (or child) documents, for which an <iframe> element is really just a kind of “mount point.”
While the utility of being able to transparently traverse across document boundaries (as easily as one might traverse across a file system mount point) is obvious, browsers likely have their reasons for blocking it.
Not the least of these, I suspect, is to prevent cross-site scripting (XSS) attacks. That is, just because the browser user has the ability to view an embedded document, doesn’t mean a script in the parent document should be able to “see” into it. And allowing traversal from the parent into the child (via, e.g., find_element_by_xpath), would likely require that.
(Another reason, I imagine, is that most modern browsers isolate each document in a separate process, making traversal across their respective DOMs a far more complicated feature to implement.)
Easing the burden with capybara-py
Given that one must switch contexts in order to search for and interact with elements in other documents, you can make it easier on yourself by adopting capybara-py. It’s a layer on top of Selenium that provides (among many other things) simple context managers for switching between frames and windows:
from capybara.dsl import page
with page.frame("name-of-child-frame"):
page.click_link("The label of a link inside the child frame")
page.assert_text("Some text that is now expected to appear in the parent document")
Unfortunately the API is built that way and you can't do anything about it. Each IFrame is a separate document as such, so eventually search a object in every IFrame would mean Selenium has to switch to every IFrame and do that for you.
Now you can build a workaround by storing the IFrame paths and using helper methods to automatically switch to that IFrame hierarchy in your code. Selenium won't help you here, but you can ease your pain by writing helper methods designed as per your needs

Edit XML in Text widget and TreeView widget at the same time

There are some applications that display XML structure in multiple views. An excellent example is Visual Studio Xaml ("Design") editor, which display an Object Tree, a rendered, "designer" window with mouse-manipulatable objects, a text editor with XAML content, and a properties window.
I am trying to do such a thing in PyQt to edit geographical maps, using KML as file format, but I would like to allow for direct editing and preservation formatting.
For example, I would like to be able to edit the formatting manually like this:
<Element attribute1="value1"
attribute2="value2"
attribute3="value3"/>
And be sure that attribute order and alignment would be kept intact upon further manipulation via TreeView or PropertiesWindow.
The fact is, a lot of answers say that, since attribute order is not a XML specification (that is, applications using XML should not count on it), the order is not guaranteed by any official library, and any library implementing it "is not doing XML". Specifically, not any Python library has support for this.
On the other hand, XAML editors in Visual Studio do that all the time.
So the question is: if I really, really want this, using Python GUI toolkits as a "support medium" (PyQt and its widgets, in principle), how hard would be the task? Should I expect to use python's XML libs, or would I have to write my own - that is, an overwhelmingly complex task - ?

Examples for Python and GtkUnixPrintDialog

I am trying to set up a print job in Python under Linux.
Now there is this GtkUnixPrintDialog that basically does what I need it to do, the problem is that I can't find any documentation on how to deal with it. I tried looking at the GTK3 reference docs, but that's all for C, and trying to translate those commands to Python didn't work.
I can easily get the dialog to open by adding it to my .glade file - but I can't find a way in Glade to connect the appropriate signals to it. Nor can I find a way to display it like other dialogs.
Anyone with a working example, to do a print job using the dialog?
Based on my previous question Creating print job in gtk3/python I have the code to set up the page as it has to be.
I am using reportlab libraries to create a pdf file, and that pdf file has to be printed out, preferably with a little control on which printer to use. Of course I could bypass that dialog entirely and dump it on the default printer, but the lack of documentation on that dialog irritates me as well.
It looks like GtkPrintUnixDialog is not included in introspection sources for GTK+. I am not sure if this is an oversight or done on purpose. However, this dialog is used internally by the higher level printing interface anyway which can be invoked with Gtk.PrintOperation.run by passing Gtk.PrintOperationAction.PRINT_DIALOG. There is a complete example in the pygobject sources.

How can I programmatically send events to Qt's webkit?

I would like to create a specialized browser that can enter certain information semi-automatically into pages that are browsed. I am using for this Qt Webkit (in particular the python bindings). How can I do this?
it's doable, using javascript code-snippets. take a look at the pyjamas-desktop "failed experiment" pyjd/pyqt4.py runtime, noting the rather important addition of the words "failed".
you would be strongly advised to avoid pywebkitqt4 for this particular purpose, instead looking at either python-hulahop (originally part of the OLPC project) or perhaps python-webkit - http://www.gnu.org/software/pythonwebkit
l.

Categories