I have been using click()
all the time through phantomJS engine on page.evaluate()
and it works just fine. but sometimes it just doesn't work I don't know why.
for example I am trying to click the button verify 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, There was no output after the click()
, I tried the same on chrome though and it was the same. any ideas or suggestions are appreciated.
Alternative of click() in Selenium We can use the JavaScript Executor to perform a click action. Selenium can execute JavaScript commands with the help of the executeScript method.
There are four typical ways to perform click in Selenium-Java bindings . WebElement button = driver. findElement(By.
The click() is only applicable to buttons with type submit in a form. The submit() function shall wait for the page to load however the click() waits only if any explicit wait condition is provided. If a form has a submit of type button, the submit() method cannot be used.
We can list the most common reasons for click problems as being one of the following: Wrong web element locations. The existence of a web element that obscures the web element that we want to click. The Selenium WebDriver works much faster than the response of the application.
ContextClick Method. Right-clicks the mouse at the last known mouse coordinates.
You can use any previously known way to click. You have two problems unrelated to the clicking itself:
page.switchToFrame(0);
Google delivers different pages depending on the capabilities of the user agent (PhantomJS in this case). It detects that PhantomJS doesn't have JavaScript enables (for some reason) and delivers a different form which doesn't have a #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 it with: PhantomJS 1.9.8 and 2.1.1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With