D
D
dmitred2018-05-10 15:58:38
PhantomJS
dmitred, 2018-05-10 15:58:38

How to login to google using Phantomjs?

Hello colleagues!
There is a task to log in to google in phantomjs.
What I tried:
I found a script on the Internet:

var webPage = require('webpage');
var page = webPage.create();

page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36';


page.open('https://accounts.google.com/ServiceLogin/identifier?hl=ru&passive=true&continue=https%3A%2F%2Fwww.google.ru%2F%3Fgws_rd%3Dssl&flowName=GlifWebSignIn&flowEntry=AddSession', function (status) {

page.evaluate(function() {
var usernameField = "Email";
var elUsername = document.getElementById(usernameField);
elUsername.value = 'xxxxxxxx';
var buttonId = "next";
var elNext = document.getElementById(buttonId);
elNext.click();
setTimeout(function(){
var passwordId = "Passwd";
var elPassword = document.getElementById(passwordId);
elPassword.value = "xxxxx";
var btnNext = document.getElementById("signIn");
btnNext.click();
},4000);
});

setTimeout(function(){

console.log(page.content);
phantom.exit();
},8000);

});

It doesn't work... I
looked at the button further, it has a different id, Google apparently changed something again div id="identifierNext" role="button", but the click does not work(
Maybe someone already has a solution?
Thanks for the help ;)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Sundukov, 2018-05-10
@dmitred

There, the id was changed not only for the next button. All IDs are different. And Further, apparently, it does not press, because the username is not entered. id for input there is now "identifierId". And the id for the password is completely auto-generated. Therefore, to begin with, you need to specify the correct id of the elements.
There is a chance that the identifiers are dynamic and will be different each time. Therefore, you need to cling to something that does not change - the text of messages visible to the user. For example, the password field should be searched starting from the div with the text "Enter your password".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question