composer require facebook/webdriver
https://goo.gl/FCSwwD
java -jar selenium-server-standalone-3.14.0.jar
https://chromedriver.storage.googleapis.com/2.9/chromedriver_win32.zip
C:\Program Files (x86)\Google\Chrome\Application
編寫測(cè)試代碼(test.php):
網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè),成都做網(wǎng)站公司-創(chuàng)新互聯(lián)公司已向上1000+企業(yè)提供了,網(wǎng)站設(shè)計(jì),網(wǎng)站制作,網(wǎng)絡(luò)營(yíng)銷等服務(wù)!設(shè)計(jì)與技術(shù)結(jié)合,多年網(wǎng)站推廣經(jīng)驗(yàn),合理的價(jià)格為您打造企業(yè)品質(zhì)網(wǎng)站。
get($baseUrl);
echo consoleText($driver->getTitle()) . "\n"; //cmd.exe中文亂碼,所以需轉(zhuǎn)碼
$topLists = $driver->findElement(WebDriverBy::className('container-top-wrapper'))->findElement(WebDriverBy::className('top-list-wrapper'))->findElements(WebDriverBy::tagName('li'));
foreach ($topLists as $topLi) {
$itemContent = $topLi->findElement(WebDriverBy::tagName('a'));
echo consoleText($itemContent->getAttribute('title')) . ' : ' . consoleText($itemContent->getAttribute('href')) . "\n";
}
//關(guān)閉瀏覽器
$driver->quit();
function consoleText($text, $pageEncoding = '', $consoleEncoding = '')
{
// windows
if (!$consoleEncoding) {
if (stristr(php_uname('s'), 'win')) {
$consoleEncoding = "GBK";
} else {
$consoleEncoding = 'UTF-8';
}
}
return exchangeEncoding($text, $pageEncoding, $consoleEncoding);
}
function exchangeEncoding($text, $pageEncoding = '', $targetEncoding = 'UTF-8')
{
if (!$pageEncoding) {
$pageEncoding = mb_detect_encoding($text, array("ASCII", 'UTF-8', "GB2312", "GBK", 'BIG5'));
}
if ($pageEncoding != $targetEncoding) {
return mb_convert_encoding($text,$targetEncoding,$pageEncoding);
}
return $text;
}
//切換至最后一個(gè)window
function switchToEndWindow($driver)
{
$arr = $driver->getWindowHandles();
foreach ($arr as $k => $v) {
if ($k == (count($arr) - 1)) {
$driver->switchTo()->window($v);
}
}
}
php test.php