Skip to content

Instantly share code, notes, and snippets.

@masonmark
Created October 5, 2011 07:07
Show Gist options
  • Save masonmark/1263833 to your computer and use it in GitHub Desktop.
Save masonmark/1263833 to your computer and use it in GitHub Desktop.
phantomjs-sandbox-woes
find_elements = (tag_name, properties = {}, die_if_not_found = true) ->
# Find all elements of type tag_name that match properties. E.g.: find_elements "input", {type: "image", src: "my_img.jpg"}. Note that if you are looking for an element by its 'src' property (e.g. for an image), be aware that the if the HTML specifies something like /foo/bar that actual property value will be something like "http://192.168.155.100/foo/bar". There may be other element properties which aren't exactly identical to what is specified in the HTML(?).
result = null
all_elements = document.getElementsByTagName(tag_name)
filtered = filter_elements all_elements, properties
if filtered.length > 0
filtered
else
if die_if_not_found
die "unable to find required page element of type '#{tag_name}' matching #{JSON.stringify properties}'"
else
[] # no match, return []
find_elements = (page, tag_name, properties = {}, die_if_not_found = true) ->
# Find all elements of type tag_name that match properties. E.g.: find_elements "input", {type: "image", src: "my_img.jpg"}. Note that if you are looking for an element by its 'src' property (e.g. for an image), be aware that the if the HTML specifies something like /foo/bar that actual property value will be something like "http://192.168.155.100/foo/bar". There may be other element properties which aren't exactly identical to what is specified in the HTML(?).
result = null
console.log "IN this context document is #{document}"
#all_elements = document.getElementsByTagName(tag_name)
all_elements = page.evaluate ->
return document.getElementsByTagName("form") # the script hangs here... because the returned object is a NodeList and not a simple array?
# NOTE: The above was my attempt, but it didn't work. :-(
console.log "all eletm: #{all_elements.length}"
filtered = filter_elements all_elements, properties
if filtered.length > 0
filtered
else
if die_if_not_found
die "unable to find required page element of type '#{tag_name}' matching #{JSON.stringify properties}'"
else
[] # no match, return []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment