Search code examples
javascriptphantomjshref

Phantomjs: Get element without id


html:

...
<a href="#" class="stuff">stuff</a>
...
<a id="yui_random_numbers" href="#" class="stuff">stuff</a> 

2nd line is from outerHTML/source which page.content doesnt provide

As far as I am aware these 'buttons' have to be clicked with a custom mouse event, but since this one doesn't have an ID I am unsure how to get the element in the first place.

code:

page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0'
page.open(address, function(status) {
    if (status === 'success') {
        page.evaluate(function() {
            click(document.getElementById('wtf is dat id doe?'))
        })
    }
})

click function looks like this one in the answer.


Solution

  • getElementById is not the only DOM selector available. You should read more about this on the MDN site like the querySelector function.

    When you will have the correct element selected, you call your click function like you do with the getElementById.