echo '打开浏览器,进行自动登录,需要2到3分钟完成...'.PHP_EOL;
$host = 'http://127.0.0.1:4444/wd/hub'; // this is the default
$capabilities = DesiredCapabilities::chrome();
// $capabilities = DesiredCapabilities::htmlUnitWithJS();
$driver = RemoteWebDriver::create($host, $capabilities,5000);
$driver->manage()->deleteAllCookies();
echo '打开qq登录页面,准备输入账号密码进行登录'.PHP_EOL;
// navigate to 'http://www.baidu.com/'
$driver->get('http://m.qzone.com');
// 输入账号密码
$user = $driver->findElement(WebDriverBy::id('u'));
$user->clear(); //将输入框清空
$user->sendKeys($uin); //在输入框中输入内容
$userId = $user->getAttribute('value'); //获取输入框的文本内容
$pass = $driver->findElement(WebDriverBy::id('p'));
$pass->clear(); //将输入框清空
$pass->sendKeys($password); //在输入框中输入内容
$password = $pass->getAttribute('value'); //获取输入框的文本内容
$ele = $driver->findElement(WebDriverBy::id('go'));
$driver->executeScript("arguments[0].click();",[$ele]);
echo '完成登录提交任务,等待网页跳转'.PHP_EOL;
$driver->wait(5)->until(
WebDriverExpectedCondition::visibilityOfElementLocated(
WebDriverBy::id('nav_bar_me')
)
);
echo '开始进入web2.qq.com'.PHP_EOL;
$driver->get('http://web2.qq.com');
$driver->wait(5)->until(
WebDriverExpectedCondition::visibilityOfElementLocated(
WebDriverBy::id('mainTopAll')
)
);
echo '开始读取配置信息'.PHP_EOL;
$vfWebQQ = $driver->executeScript('return mq.vfwebqq');
$ptWebQQ = $driver->executeScript('return mq.ptwebqq');
$pSessionId = $driver->executeScript('return mq.psessionid');
$uin = $userId;
echo '登录信息如下'.PHP_EOL;
echo ($vfWebQQ).PHP_EOL;
echo ($pSessionId).PHP_EOL;
echo ($ptWebQQ).PHP_EOL;
// close the browser
echo '登录完成,正在关闭浏览器...'.PHP_EOL;
$cookies = $driver->manage()->getCookies();
$driver->quit();
echo '保存用户登录信息'.PHP_EOL;
$newCookie = new CookieJar();
foreach ($cookies as $key => $value) {
$cookie = current($value);
$newCookie->setCookie(new SetCookie([
'Domain' => $value['domain'],
'Name' => $value['name'],
'Value' => $value['value'],
'Expires' => $value['expiry'],
'Path' => $value['path'],
]));
}
$this->cookies = $newCookie;
$this->credential = new Credential($ptWebQQ, $vfWebQQ, $pSessionId, $uin, static::$clientId, $this->cookies);
//获取在线状态避免103
$this->getFriendsOnlineStatus();
return $this->credential;
}`