Php使用php-webdriver库调用Chrome浏览器无头模式的2种方法

一、安装Chrome浏览器

参考《Linux环境centos7系统,宝塔面板如何安装老版本谷歌chrome浏览器》

二、安装PHP Webdriver库

cd /home/wwwroot/testjs   //切换到网站根目录
composer require facebook/webdriver //已过期
composer require php-webdriver/webdriver //使用这个命令安装php webdriver

方法一:php-webdriver库利用Chromedriver来调用Chrome浏览器
只需要再执行步骤三安装Chromedriver

三、安装浏览器驱动程序Chromedriver并启动

114之前chromedriver驱动版本
官方:https://chromedriver.storage.googleapis.com/index.html
国内镜像:https://registry.npmmirror.com/binary.html?path=chromedriver/

115以后chromedriver驱动版本
国内镜像:https://registry.npmmirror.com/binary.html?path=chrome-for-testing/

这里我选择的是和已安装chrome浏览器相近版本124.0.6367.207,点击进入对应目录,下载chromedriver-linux64.zip到本地,然后解压文件,将里面的文件chromedriver传到服务器目录/usr/bin

选择和Chrome浏览器相近的chromedriver版本

-- 如果PATH环境变量正确,则可以查看到chromedriver版本
chromedriver --version
chromedriver --port=4444 //PHP Webdriver调用chromedriver之前,请先指定端口并启动

-- 如果不正确,可以通过下面方法,添加chromedriver对应的目录到环境变量中,比如
echo $PATH //查看有哪些目录在环境变量中
echo 'export PATH=$PATH:/path/to/driver' >> ~/.bash_profile //配置PATH环境变量
source ~/.bash_profile

使用Chrome浏览器无头模式测试代码,保存为test.php,然后通过网址/test.php访问

<?php
require_once 'vendor/autoload.php';

use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverBy;

use Facebook\WebDriver;
use Facebook\WebDriver\Remote;

// 设置 ChromeOptions 以启用无头模式
$options = (new ChromeOptions())->addArguments([
'headless', // 启用无头模式
'disable-gpu', // 适用于 Linux,禁用 GPU 加速以避免问题
'no-sandbox' // 在 Docker 等环境中可能需要禁用沙箱
]);

// 设置 DesiredCapabilities
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY, $options);

// 创建 WebDriver 实例
$host = 'http://localhost:4444'; // 如果使用 Selenium Grid,则需要更改主机和端口
$driver = RemoteWebDriver::create($host, $capabilities);

try {
// 打开一个网页
$driver->get('https://www.baidu.com/');
print_r($driver->getPageSource());
// 执行一些操作,例如获取页面标题
//echo $driver->getTitle() . "\n";
/*
$search_box = $driver->findElement(WebDriverBy::id('kw'));
$search_box->sendKeys('WebDriver');
$search_button = $driver->findElement(WebDriverBy::id('su'));
$search_button->click();
$results = $driver->findElements(WebDriverBy::cssSelector('#content_left .result .t a'));
foreach ($results as $result) {
echo $result->getText() . PHP_EOL;
}
*/

} finally {
// 关闭浏览器
$driver->quit();
}

执行正确,获取到百度首页,然后打印出来如下

方法二,php-webdriver库利用Selenium Grid来调用Chrome浏览器
只需要再执行步骤四安装Selenium Grid即可

四、安装Selenium Grid

Selenium Server jar文件下载地址,经测试如果是单机部署请下载历史版本里的单机版,否则报错

最新版本:https://www.selenium.dev/downloads/
或者:https://github.com/SeleniumHQ/selenium/releases/latest

selenium-server所有历史版本及单机版:
https://github.com/SeleniumHQ/selenium/releases
或者:https://registry.npmmirror.com/binary.html?path=selenium/
或者:https://selenium-release.storage.googleapis.com/index.html

-- Selenium Grid需要java 11及以上版本支持,如果没有安装请先安装
sudo yum install java-11-openjdk //安装 Java 11 或更高版本
java -version //查看java版本

cd /www/server //进入自己的服务器文件夹
mkdir selenium-server //新建一个文件夹用来存放selenium-server服务器
cd selenium-server //进入文件夹

-- 下载和启动
wget -O selenium-server-standalone-4.0.0-alpha-2.jar https://registry.npmmirror.com/-/binary/selenium/4.0/selenium-server-standalone-4.0.0-alpha-2.jar
java -jar selenium-server-standalone-4.0.0-alpha-2.jar //在selenium-server目录下,单机版本可以用这个命令启动,不用加standalone
java -jar selenium-server-4.30.0.jar standalone //在selenium-server目录下,运行命令启动单机部署(Standalone)


-- 测试是否正确
curl http://localhost:4444/wd/hub/status

只需要将上文中的测试代码里面的地址改成下面的就行,即加上“/wd/hub”部分,最后访问的效果和方法一一样

$host = 'http://localhost:4444/wd/hub'; // 如果使用 Selenium Grid,则需要更改主机和端口

相关推荐