Search code examples
javascripthtmldomspynner

Spynner - accessing DOM


I've just learned that there is a difference between a browser's "view source" and "inspect element". When I use "inspect" in firefox on a specific webpage I get the following:

<html>
 ...
    <div class="someClass" id="someID" style="z-index: 12001; left: 0px; top: -487px; width: 1288px; height: 843px; opacity: 0.7; visibility: visible;">
 ...
</html>

However, when I use "view source" I don't see this element. The problem of not seeing the element occurs, when I access the webpage via Spynner and parse the html. Since I need to know the value of "visibility", my question is: how can I access the DOM within Spynner?

I've already tried the following source code:

br = spynner.Browser()
br.show()
br.load(url)
ret = br.runjs('document.getElementById('someID');")
print ret

But this will only print out:

<PyQt4.QtCore.QVariant object at 0x9870e64>

//

EDIT

Since ret is a QVariant I managed to turn it into a PyObject by modifying my code:

ret = br.runjs('document.getElementById('someID').getAttribute('style');")
print ret.toPyObject()

The only problem I have right now is: print only displays the first value of "style"

z-index: 9999;

Can I somehow access the other values (left, top, height, opacity, visibility)?


Solution

  • I found out that I messed with my code. Instead of "someID" I inserted "someOtherID", which didn't contain the attribute I was looking for. This stupid mistake took me a lot of time.

    Then the other thing is: the element I was looking for is a java PopUp. If a user gives input, which is not allowed, then this PopUp will show up and needs to be confirmed. So what I've done now instead of checking if the PopUp is "visible", I check if the confirmation button is "not disabled". This is done via:

    val items = document.getElementsByTagName('input');
    
    for(var i=0; i<items.length; i++){
      if(items[i].getAttribute('name') == inputName){
        if(items[i].getAttribute('disabled') == null){
          items[i].click();}}}
    

    This js-code gets called via the runjs()-method inside spynner. The solution may be pedestrian, but it works for me. Also I know that this solution is very specific. But maybe someday someone might have a similar problem.