Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative way for click()

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.

like image 399
Skyliquid Avatar asked May 08 '16 11:05

Skyliquid


People also ask

What is the alternative for click ()?

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.

How many ways we can perform click?

There are four typical ways to perform click in Selenium-Java bindings . WebElement button = driver. findElement(By.

What is the difference between click () and submit in Selenium?

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.

Why click method is not working in Selenium?

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.

What is ContextClick () used for?

ContextClick Method. Right-clicks the mouse at the last known mouse coordinates.


1 Answers

You can use any previously known way to click. You have two problems unrelated to the clicking itself:

  • Elements can only be accessed in their respective Document. ReCAPTCHA is always loaded in an iframe, which is a different Document that you have to switch to. For example: 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

like image 88
Artjom B. Avatar answered Nov 12 '22 08:11

Artjom B.