为了避免本地Selenium服务器的稳定性问题,以及解决内网的可用性问题,采用Docker将Selenium服务安装在在线节点上,方便程序随时调用。
环境
- Debian 10
1.安装Docker
1 2 3 4 |
apt-get remove docker docker-engine docker.io containerd runc curl -fsSL https://download.docker.com/linux/debian/gpg | sudo apt-key add - apt-get update apt-get install docker-ce docker-ce-cli containerd.io |
2.运行image
1 |
docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome:3.141.59-zirconium |
3.安装Facebook\WebDriver
1 |
composer require php-webdriver/webdriver |
3.连接远程Selenium节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<?php // include composer autoload require_once 'vendor/autoload.php'; use Facebook\WebDriver\Remote\DesiredCapabilities; use Facebook\WebDriver\Remote\RemoteWebDriver; use Facebook\WebDriver\WebDriverBy; use Facebook\WebDriver\Exception\NoSuchElementException; use Facebook\WebDriver\Exception\WebDriverCurlException; use Facebook\WebDriver\Exception\HttpCommandExecutor; use Facebook\WebDriver\chrome\ChromeOptions; $host = 'http://localhost:4444/wd/hub'; $options = new ChromeOptions(); $options->addArguments([ '--aggressive-cache-discard', '--disable-gpu-program-cache', '--disable-gpu-shader-disk-cache', '--gpu-program-cache-size-kb=1', '--mem-pressure-system-reserved-kb=1', '--disk-cache-dir=null' ]); $caps = DesiredCapabilities::chrome(); $caps->setCapability(ChromeOptions::CAPABILITY, $options); $driver = RemoteWebDriver::create($host, $caps); try { $result = 0; // visit target $driver->get('https://www.amazon.co.jp/gp/offer-listing/B0141ZPO1E/ref=dp_olp_new?ie=UTF8&condition=new'); // search price $element = $driver->findElement(WebDriverBy::xpath('//*[@id="olpOfferList"]/div/div/div[2]/div[1]/span[1]')); $result = $element->getText(); echo $result.'<br/>'; } catch ( \Exception $e) { echo 'Exception: '.json_encode( $e->getResults() ).'<br/>'; } ob_flush(); flush(); sleep(10); } |
查看正在运行的浏览器进程
1 |
http://localhost:4444/wd/hub |
There are no comments yet