一、在linux上
1.安装谷歌浏览器教程
https://www.cnblogs.com/guodoudou/p/13498039.html
下载谷歌浏览器chrom
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
yum install -y lsb
yum localinstall google-chrome-stable_current_x86_64.rpm
查看版本
google-chrome --version
2.安装谷歌驱动
注意:选择谷歌浏览器(v92.0.4515.43)要下载与之对应的驱动(v92.0.4515.43)
#下载驱动(对应版本)
http://chromedriver.storage.googleapis.com/index.html
unzip chromedriver_linux64.zip
添加到环境变量
mv chromedriver_linux64 /usr/local/bin
?chromedriver --version
3.添加谷歌浏览器驱动进程到supervisor
注意:需要指定LANGUAGE=ZH-CN.UTF-8 ,否则有部分cookie会获取不到(踩坑过)
chromedriver --port=4444 #指定端口监听进程
[program:chrome-driver]
command=LANGUAGE=ZH-CN.UTF-8 chromedriver --port=4444
process_name=chromedriver
user=wcloud
autostart=true
autorestart=true
startretries=3
stdout_logfile_maxbytes=10MB
stdout_logfile=/path-to-log/%(program_name)s.log
stderr_logfile=/path-to-log/%(program_name)s_error.log
supervisor的相关安装教程:https://www.kancloud.cn/linjinkun/mysql1/2185845
4.环境ok后,就是上代码了
详细教程:https://github.com/php-webdriver/php-webdriver
#composer 拉取代码
php require php-webdriver/webdriver
demo.php (自己总结的)
<?php
// An example of using php-webdriver.
// Do not forget to run composer install before. You must also have Selenium server started and listening on port 4444.
namespace Facebook\WebDriver;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
require_once('../vendor/autoload.php');
// This is where Selenium server 2/3 listens by default. For Selenium 4, Chromedriver or Geckodriver, use http://localhost:4444/
$host = 'http://localhost:4444';
$capabilities = DesiredCapabilities::chrome();
// 非windows系统浏览器不提供可视化页面
if ('WIN' !== strtoupper(substr(PHP_OS, 0, 3))) {
$options = new ChromeOptions();
$options->addArguments(
[
'--no-sandbox', // 解决DevToolsActivePort文件不存在的报错
'window-size=1080x1920', // 指定浏览器分辨率
'--disable-gpu', // 谷歌文档提到需要加上这个属性来规避bug
'--hide-scrollbars', // 隐藏滚动条, 应对一些特殊页面
'blink-settings=imagesEnabled=false', // 不加载图片, 提升速度
'--headless', // 浏览器不提供可视化页面
]
);
$capabilities->setCapability(ChromeOptions::CAPABILITY, $options);
}
$driver = RemoteWebDriver::create($host, $capabilities);
// navigate to Selenium page on Wikipedia
$driver->get('https://en.wikipedia.org/wiki/Selenium_(software)');
// write 'PHP' in the search box
$driver->findElement(WebDriverBy::id('searchInput')) // find search input element
->sendKeys('PHP') // fill the search box
->submit(); // submit the whole form
// wait until 'PHP' is shown in the page heading element
$driver->wait()->until(
WebDriverExpectedCondition::elementTextContains(WebDriverBy::id('firstHeading'), 'PHP')
);
// print title of the current page to output
echo "The title is '" . $driver->getTitle() . "'\n";
// print URL of current page to output
echo "The current URL is '" . $driver->getCurrentURL() . "'\n";
// find element of 'History' item in menu
$historyButton = $driver->findElement(
WebDriverBy::cssSelector('#ca-history a')
);
// read text of the element and print it to output
echo "About to click to button with text: '" . $historyButton->getText() . "'\n";
// click the element to navigate to revision history page
$historyButton->click();
// wait until the target page is loaded
$driver->wait()->until(
WebDriverExpectedCondition::titleContains('Revision history')
);
// print the title of the current page
echo "The title is '" . $driver->getTitle() . "'\n";
// print the URI of the current page
echo "The current URI is '" . $driver->getCurrentURL() . "'\n";
// delete all cookies
$driver->manage()->deleteAllCookies();
// add new cookie
$cookie = new Cookie('cookie_set_by_selenium', 'cookie_value');
$driver->manage()->addCookie($cookie);
// dump current cookies to output
$cookies = $driver->manage()->getCookies();
print_r($cookies);
// close the browser
$driver->quit();
二、在windows上
1.查看谷歌浏览器的版本号
?2.下载对应谷歌浏览器驱动
http://chromedriver.storage.googleapis.com/index.html
或者https://npm.taobao.org/mirrors/chromedriver
?3.启动驱动,双击.exe
?4.同上
|