petecoop / phantasma

A high level promise based wrapper for phantomjs
ISC License
33 stars 8 forks source link

Working with more convoluted selectors #25

Closed flc-unholster closed 8 years ago

flc-unholster commented 9 years ago

Hello, the sample script for DuckDuckGo works perfectly, but what if I need to click on an element with a more convoluted path?

.click('#search_button_homepage') // cool, you have an id

I need to click on a "next page" link. I can get it with this jQuery/Sizzle code:

$($('div.paging')[0]).children().children().children('a')[0]

Or like this too:

$('div.paging > ul > li > a' )[0]

I can provide the URL to the page I'm working on if it helps, but I'm more interested in a general answer: how to get any element, no matter how deeply nested it is, when there's no handy id to get it.

petecoop commented 9 years ago

We're using document.querySelector() to find the element, so your second example should work e.g.

.click('div.paging > ul > li > a')

or if you want a specific index in the list:

.click('div.paging > ul > li:nth-child(3) > a')