我不知道我的逻辑是否正确,但我正在考虑使用phantomjs无头浏览器和php-webdriver来解决使用限制或无法通过auth登录页面(例如facebook)登录的问题。
很好,第一个实例会在访问网址时将用户代理设置为浏览器的代理,但可以通过重定向(通过元或javascript重定向)到另一个页面来验证useragent以及饼干。
我从bpteam / php-cookie中提取了一个代码片段,以从预配置的Cookie文件中获取Cookie,如下所示
<?php
$cookiedata = file_get_contents("/usr/share/nginx/html/php-webdriver/cookies.txt");
//from bpteam/php-cookie
function parsCookieString($text){
$parameters = ['expires', 'domain', 'path'];
if(preg_match('%^\s*(?<name>\w+)\s*=\s*(?<value>[^;]+)%ims', $text, $match)){
$cookie['name'] = trim($match['name']);
$cookie['value'] = trim($match['value']);
} else {
return false;
}
foreach($parameters as $param){
if(preg_match('%' . $param . '\s*=\s*(?<val>[^;]+)%i', $text, $match)){
$cookie[$param] = trim($match['val']);
}
}
$cookie['secure'] = (bool)preg_match('%;\s*secure\s*(;|$)%i', $text);
$cookie['httponly'] = (bool)preg_match('%;\s*httponly\s*(;|$)%i', $text);
return $cookie;
}
function from($text){
$lines = explode("\n", $text);
$regexDelimiter = '(?:(?:\\\\n)?((\\\\0|\\\\{2}|((\\\\x[0-9a-f]{2}){2}|\\\\x[0-9a-f]{2})|\\\\x[0-9a-f]|\\\\_|\\\\[abtnvfr]|[g-zG-Z]|\W|\\\\\W)))';
$regexLine = "%^cookies=\"?\@Variant\(({$regexDelimiter}{4}){2}QList\\<QNetworkCookie\\>\\\\0({$regexDelimiter}{4}){2}(?<cookie_str>.*)\)\"?\s*$%ms";
if(!isset($lines[1]) || !preg_match($regexLine, $lines[1], $match)){
return array();
}
$delimiter = 'REPLACE_COOKIE_DELIMITER';
$regEx = "\\\\0\\\\0\\\\0".$regexDelimiter;
$text = preg_replace("%$regEx%ms", $delimiter, $match['cookie_str']);
$cookiesLines = explode($delimiter, $text);
$cookies = array();
foreach ($cookiesLines as $cookieLine) {
$cookie = parsCookieString($cookieLine);
if($cookie){
$cookies[] = $cookie;
}
}
return $cookies;
}
$cookies = from($cookiedata);
?>
这就是我试图通过auth登录页面(例如facebook)登录时无法成功的事情。
<?php
set_time_limit(0);
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\WebDriverCapabilityType;
use Facebook\WebDriver\WebDriverDimension;
use Facebook\WebDriver\WebDriverBy;
require_once('vendor/autoload.php');
//require_once('visidom.php');
require_once('cookie.php');
$host = 'http://localhost:4444/wd/hub';
$capabilities = DesiredCapabilities::phantomjs();
$capabilities->setCapability(
'phantomjs.cli.args',
['--ignore-ssl-errors=true', '--web-security=false','--cookies-file=/usr/share/nginx/html/php-webdriver/cookies.txt','--webdriver=4444']
);
$capabilities->setCapability(
'phantomjs.page.settings.userAgent',
'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36'
);
$driver = RemoteWebDriver::create($host, $capabilities,5000);
$window = new WebDriverDimension(1024, 768);
$driver->manage()->window()->setSize($window);
@chmod('/usr/share/nginx/html/php-webdriver/cookies.txt',0777);
$driver->get('https://facebook.com');
foreach($cookies as $cookie)
$driver->manage()->addCookie($cookie); //here i'm trying to add cookie
$driver->wait(10,1000)->until(
WebDriverExpectedCondition::presenceOfAllElementsLocatedBy(
WebDriverBy::xpath('.//input[@id="email"]')
)
);
$inputUsername = $driver->findElement(
WebDriverBy::xpath('.//input[@id="email"]')
);
$inputUsername->sendKeys('<your username>');
$driver->wait(10,1000)->until(
WebDriverExpectedCondition::presenceOfAllElementsLocatedBy(
WebDriverBy::xpath('.//input[@id="pass"]')
)
);
$inputPassword = $driver->findElement(
WebDriverBy::xpath('.//input[@id="pass"]')
);
$inputPassword->sendKeys('<your password>');
$driver->wait(10,1000)->until(
WebDriverExpectedCondition::presenceOfAllElementsLocatedBy(
WebDriverBy::xpath('.//label[@id="loginbutton"]')
)
);
$button = $driver->findElement(
WebDriverBy::xpath('.//label[@id="loginbutton"]')
);
$button->click();
$driver->takeScreenshot('./screen.png');
?>
如何跟踪页面重定向并在phantomjs中持久设置常量浏览器用户代理并监视响应标头以不断向浏览器设置cookie信息?
答案 0 :(得分:0)
出现这种困难是因为幻影并没有保留会话cookie,因此我按照此链接https://github.com/ariya/phantomjs/issues/12277提供的解决方案来解决问题。