Dumping browser document content using Zombie.js
Asked Answered
I

4

6

Using browser.visit, I am fetching the page of a browser as shown in the documentation. According to the browser API, browser.document returns the main window's document. However, I am not sure how to dump (display) the contents of the document. Is there a method like browser.document.toString() or browser.document.text() to be able to print the contents of the document in the console.

Thanks, Sony

Irritated answered 8/5, 2011 at 8:32 Comment(2)
did you try browser.html()?Mature
Yes, I did. But, it prints the entire html contents, right? I thought browser encompasses all details of the page such as state, content etc. whereas the document is a subset which just stores the dom structure. Am I wrong? I am trying to print just the dom structure using browser.document as against the entire html.Irritated
A
5

What you want is probably:

browser.document.innerHTML 
Alsatia answered 9/4, 2012 at 9:49 Comment(3)
BTW, this doesn't seem to work on the current version of Zombie.js, but browser.document.documentElement.innerHTML does ;)Doubleminded
undefinedundefinedundefinedundefinedChitwood
hence the probably keyword :)Alsatia
A
3

There is a browser.text(selector, context?). Selector is a CSS selector evaluated against the document body. Context is a optional second argument, the CSS selector is evaluated against the element given as the context.

You can say something like browser.text('body') to get the text in the body.

Aeroballistics answered 8/5, 2011 at 8:39 Comment(2)
browser.text('document') does not work. I thought document is a selector of browser. I am trying to access the "dom strucuture" via browser.document. - SonyIrritated
@Irritated browser.text('body') will select whatever text is in the body tag of a html page/document. I haven't tried it but you should be able to do browser.text('html') to select the text in the headers + body.Howlyn
B
2

I got here while looking for answer for the same question. I may be late for the party, but try using

Browser.visit(url, function(error, browser){
    fs.appendFileSync('index.html', browser.html());
})

Remember to put error checking here and do better handling, but should give you basic HTML document.

Bixby answered 21/12, 2015 at 14:14 Comment(0)
N
1

If it's not necessarily HTML (like you find yourself pulling XML or JSON through Zombie due to complicated, valid reasons...), you can access it like this:

browser.document._childNodes[0]._nodeValue
Nightie answered 25/5, 2012 at 19:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.