xpathcoffeescriptweb-scrapingcasperjsspookyjs

All XPaths return a non-existent error in CasperJS?


Just to be clear I am using SpookyJS which is a library that allows for a headless CasperJS.

I am able to click and select other XPaths just fine on all other pages,the problem is only on a particular page, the page loads perfectly but all of the XPaths return this error.

Cannot dispatch mousedown event on nonexistent selector

I have a screenshot taken before the function attempts to click the xPath and the screenshot shows that the page is loaded perfectly.

if I trying using the waitForSelector function I get the timeout error, I've tried different XPaths on different pages and none of them work.

Here is my code in CoffeeScript don't mind the spooky.then just think of it as casper.then:

// 3 steps occur before this and they work perfectly
spooky.then([{x:selectXPath}, () ->
  @wait(3000, () ->
    eval(x) // This loads the xPath function
    @capture('server/components/spooky/img.png')
    @click(xPath('//*[@id="wp-page-header-middle"]/table/tbody/tr/td[1]/a'))
  )
])

The table I'm interested in is inside of an iframe.


Solution

  • The problem is that the element is inside of an iframe. The element can be selected, but you first need to switch into the context of the iframe to run operations on it. This is done through withFrame(). You can either select the iframe by index or name. Here is an example by index (first iframe):

    @withFrame(0, () ->
        @click(xPath('//*[@id="wp-page-header-middle"]/table/tbody/tr/td[1]/a'))
    )
    

    You may also need to adjust your XPath, because the tbody may not be present in the markup initially. PhantomJS 1.x doesn't add it like modern browsers do, so you may need to do this dynamically:

    '//*[@id="wp-page-header-middle"]/table//tr/td[1]/a'