《使用Nodejs用selenium webdriver 模拟登录阿里妈妈平台》要点:
本文介绍了使用Nodejs用selenium webdriver 模拟登录阿里妈妈平台,希望对您有用。如果有疑问,可以联系我们。
相关主题:淘客API和高佣转链
如何使用Nodejs用selenium webdriver 模拟登录阿里妈妈平台?
注意,LINUX中需要有图形桌面,无界面另文介绍
准备:
(1)到 https://github.com/mozilla/geckodriver/releases/ 下载WINDOW或LINUX相应包,放到系统变量PATH能找到的地方,释放其中的exe
LINUX:
# wget https://github.com/mozilla/geckodriver/releases/download/v0.20.1/geckodriver-v0.20.1-linux64.tar.gz
WINDOW下载并释放EXE到node.js安装目录下。
https://github.com/mozilla/geckodriver/releases/download/v0.20.1/geckodriver-v0.20.1-win64.zip
(2)安装模块:
$ npm install selenium-webdriver --save -g
(3)执行
$ node login.js
会弹出firefox模拟登录。自动录入帐密
#====================== 代码,请保存为login.js ========================#
var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until;
//chrome
var driver = new webdriver.Builder()
.forBrowser('firefox')
.build();
var userName = 'www.vephp.com';
var passwrod = 'xxx';
var loginUrl = 'https://login.taobao.com/member/login.jhtml?style=mini&from=alimama&redirectURL=http%3A%2F%2Flogin.taobao.com%2Fmember%2Ftaobaoke%2Flogin.htm%3Fis_login%3d1&full_redirect=true&disableQuickLogin=true';
driver.get(loginUrl)
.then(function () {
return driver.findElement(By.id('TPL_username_1')).sendKeys(userName);
})
.then(function () {
return driver.findElement(By.id('TPL_password_1')).sendKeys(passwrod);
})
.then(function () {
return driver.findElement(By.id('J_SubmitStatic')).click();
})
.then(function () {
sleep(8000);
driver.navigate().to('http://pub.alimama.com/');
})
.then(function () {
return driver.manage().getCookies().then(function (cookies) {
console.log(cookies);
var map = buildCookieMap(cookies);
var cookie2 = map.cookie2.value;
return cookie2;
});
})
.then(function (cookie2) {
//cookie2的值
console.log(cookie2);
});
function buildCookieMap(cookies) {
var map = {};
cookies.forEach(function (cookie) {
map[cookie.name] = cookie;
});
return map;
}
function sleep(d) {
for (var t = Date.now(); Date.now() - t <= d;);
}
-----------------------------
执行结果:
M:\apacheWeb\216>node logintb.js
[ { name: 'cookieCheck',
value: '7856',
path: '/',
domain: '.login.taobao.com',
expiry: null,
secure: false,
httpOnly: false },
{ name: 'v',
value: '0',
path: '/',
domain: '.taobao.com',
expiry: null,
secure: false,
httpOnly: false },
{ name: 'cookie2',
value: '1a6e5c4ad16f581a3922ccbffde75232',
path: '/',
domain: '.taobao.com',
expiry: null,
secure: false,
httpOnly: true },
{ name: 't',
value: '8d24807e818aeb00dc1b30a96f5f009d',
path: '/',
domain: '.taobao.com',
expiry: 1532604923,
secure: false,
httpOnly: false },
{ name: '_tb_token_',
value: 'e4bd8e30f9955',
path: '/',
domain: '.taobao.com',
expiry: null,
secure: false,
httpOnly: false },
{ name: '_uab_collina',
value: '152482876230680480766222',
path: '/member/',
domain: 'login.taobao.com',
expiry: 1840188762,
secure: false,
httpOnly: false },
{ name: 'cna',
value: '+/hpE9+UQRICATs4UhVC2PLL',
path: '/',
domain: '.taobao.com',
expiry: 2155548762,
secure: false,
httpOnly: false },
{ name: 'isg',
value: 'BLm5VBPqx0sNMZvfQcQnQ1jIyyZTbqx6_fqcLdvuNeBfYtn0Ixa9SCcw4OaUQUWw',
path: '/',
domain: '.taobao.com',
expiry: 1540380763,
secure: false,
httpOnly: false },
{ name: 'um',
value: '243995BB5F7510FD18BB9D81386C276387C4C23B69DDE462A3BA43ACCF6E9182CE68
C44176FF0A13CD43AD3E795C914C3E3833C13E62806CA43ACED7C1B527A6',
path: '/',
domain: '.taobao.com',
expiry: 1556364766,
secure: false,
httpOnly: false } ]
1a6e5c4ad16f581a3922ccbffde75232