PhantomJS; click an element

前端 未结 11 857
借酒劲吻你
借酒劲吻你 2020-11-21 23:53

How do I click an element in PhantomJS?

page.evaluate(function() {
    document.getElementById(\'idButtonSpan\').click();  
});

This gives

11条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2020-11-22 00:14

    It's not pretty, but I've been using this to allow me to use jQuery for the selection:

    var rect = page.evaluate(function() {
        return $('a.whatever')[0].getBoundingClientRect();
    });
    page.sendEvent('click', rect.left + rect.width / 2, rect.top + rect.height / 2);
    

    but you can always replace $(s)[0] with document.querySelector(s) if not using jQuery.

    (It does rely on the element being in view mind, i.e. your viewportSize.height is big enough).

提交回复
热议问题