$ npm install selenium-webdriver --save
淘客开发者要模拟登录阿里妈妈是常见的,可利用Node.js+selenium webdriver。
以上是环境必需,下面是代码:不过实际应用,还需要做一些小修改:
var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until;
//chrome
var driver = new webdriver.Builder()
.forBrowser('firefox')
.build();
var userName = '';
var passwrod = '';
var loginUrl = 'https://login.taobao.com/member/login.jhtml?style=mini&newMini2=true&css_style=alimama&from=alimama&redirectURL=http%3A%2F%2Fwww.alimama.com&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) {
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;);
}