1.composer安裝selenium:
composer? require? facebook/webdriver
2.安裝phantomjs
http://phantomjs.org/download.html 下載對應(yīng)版本,并加入到環(huán)境變量
3.下載Selenium Standalone Server(http://www.seleniumhq.org/download/?)。
到命令提示符里啟動以下命令(前提需要安裝jdk,確保java命令能夠運行)
java -jar selenium-server-standalone-2.50.0.jar
tip:最新版 selenium-server-standalone-3.13.0.jar 貌似放棄支持phantomjs了,所以用的低版本的
貼代碼:
header("Content-Type: text/html; charset=UTF-8");
$host = 'http://localhost:4444/wd/hub'; // this is the default
$capabilities = DesiredCapabilities::phantomjs();
$driver = RemoteWebDriver::create($host, $capabilities, 5000);
$driver->get('https://www.zhihu.com/question/285353546/answer/443155679');
$ele = $driver->findElement(WebDriverBy::className('QuestionMainAction'));
$driver->executeScript("arguments[0].click();",[$ele]);
//翻一頁
$js = "window.scrollTo(0,document.body.scrollHeight)"; //滾動至底部
$driver->executeScript($js);
$driver->takeScreenshot('test.png');
//關(guān)閉瀏覽器
$driver->quit();
可實現(xiàn)用戶點擊事件,然后下拉到底,整頁截圖。(chromedriver等其他無頭瀏覽器也可以實現(xiàn)截圖,但是截的是一個屏幕的,不是整個頁面的,有大神實現(xiàn)是用拼接的方式實現(xiàn)的,其他方法還沒有發(fā)現(xiàn))