Alternative way to click ()

I use click() all the time through the phantomJS engine on page.evaluate() and it works fine. but sometimes it just doesn’t work, I don’t know why.

For example, I'm trying to click a button here

I tried this:

 page.evaluate(function() { document.getElementById('recaptcha-verify-button').click(); }); 

and this:

 rect = page.evaluate(function() { return document.getElementById('recaptcha-verify-button').getBoundingClientRect(); }); console.log(rect.left + " " + rect.right); page.sendEvent('mousemove', rect.left + rect.width / 2, rect.top + rect.height / 2); page.sendEvent('mousedown', rect.left + rect.width / 2, rect.top + rect.height / 2); page.sendEvent('mouseup', rect.left + rect.width / 2, rect.top + rect.height / 2) 

Both did not work, after click() there was no way out, I tried the same on chrome, although it was the same. Any ideas or suggestions are welcome.

+7
javascript phantomjs
source share
3 answers

Here is a function to simulate clicking on an element

 function simulateClick(element) { var event = document.createEvent("MouseEvents"); event.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); element.dispatchEvent(event); } 

I think this is what you need to solve the problem.

-one
source share

You can use any previously known click method . You have two non-click issues:

  • Elements may only be available in the relevant document. ReCAPTCHA is always loaded in an iframe, which is another document that you should switch to. For example: page.switchToFrame(0);
  • Google provides different pages depending on the capabilities of the user agent (PhantomJS in this case). It discovers that PhantomJS does not have JavaScript (for some reason), and supplies another form that does not have the #recaptcha-verify-button element. Instead, it has:

     <div class="fbc-button-verify"> <input type="submit" value="Verify"> </div> 

Full script:

 var page = require('webpage').create(); page.open('https://www.google.com/recaptcha/api2/demo', function() { page.render('1.png'); page.switchToFrame(0); page.evaluate(function(){ document.querySelector('.fbc-button-verify > input').click(); }); setTimeout(function(){ page.render('2.png'); phantom.exit(); }, 3000); }); 

Tested with: PhantomJS 1.9.8 and 2.1.1

+3
source share

I'm not sure I understand your problem, but someday you had an unexpected problem with the identifier, because id must uniq and some engine make the element again, and this may be the reason.

Please tell me if I made a mistake in understanding the problem.

I think you should use class instead of id, and you can do it simply with jquery
look at this example:

 var tests = document.querySelectorAll('.test'); for(var i=0; i<tests.length; i++){ tests.item(i).click(); } 
 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> </head> <body> <div class="test" onclick="alert(1);">test 1</div> <div class="test" onclick="alert(2);">test 2</div> </body> </html> 
-3
source share

All Articles