selenium部署
- 安装selenium:
composer require facebook/webdriver
- 下载jdk(略)
- 下载selenium服务:
https://goo.gl/FCSwwD
- 运行selenium服务:
java -jar selenium-server-standalone-3.14.0.jar
- 下载chromedriver:
https://chromedriver.storage.googleapis.com/2.9/chromedriver_win32.zip
- 将chromedriver放到chrome安装目录下:
C:\Program Files (x86)\Google\Chrome\Application
- 移动chromedriver到chrome安装目录:
- 添加chrome安装目录下环境变量:
编写测试代码(test.php):
网站建设、成都网站建设,成都做网站公司-创新互联公司已向上1000+企业提供了,网站设计,网站制作,网络营销等服务!设计与技术结合,多年网站推广经验,合理的价格为您打造企业品质网站。
get($baseUrl); echo consoleText($driver->getTitle()) . "\n"; //cmd.exe中文乱码,所以需转码 $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"; } //关闭浏览器 $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; } //切换至最后一个window function switchToEndWindow($driver) { $arr = $driver->getWindowHandles(); foreach ($arr as $k => $v) { if ($k == (count($arr) - 1)) { $driver->switchTo()->window($v); } } }
- 运行测试代码:
php test.php
网页题目:selenium部署
分享路径:http://cdiso.cn/article/iijsii.html