前提:已经安装好宝塔面板、php-webdriver
如果没有安装,请在本站搜索webdriver,里面有详细教程
1.下载火狐浏览器
直接下载最新版的火狐浏览器进行安装
2.下载Geckodriver
进网址https://github.com/mozilla/geckodriver/releases下载最新版的驱动并打开
3.测试
根目录新建一个文件夹,在里面再新建一个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\Remote\RemoteWebDriver;
use Facebook\WebDriver\Firefox\FirefoxOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
require_once('../vendor/autoload.php'); //注意这个位置,我是在网站根目录额外创建了一个文件夹,所以有../,请根据实际位置修改
$serverUrl = 'http://localhost:4444';
$desiredCapabilities = DesiredCapabilities::firefox();
// Disable accepting SSL certificates
$desiredCapabilities->setCapability('acceptSslCerts', false);
// Add arguments via FirefoxOptions to start headless firefox
$firefoxOptions = new FirefoxOptions();
$firefoxOptions->addArguments(
[
//'--headless', // 浏览器不提供可视化页面
//'--incognito', //无痕模式启动
]
);
$desiredCapabilities->setCapability(FirefoxOptions::CAPABILITY, $firefoxOptions);
$driver = RemoteWebDriver::create($serverUrl, $desiredCapabilities);
$url = $_GET['url'];
// navigate to Selenium page on Wikipedia
$driver->get($url);
$content = $driver->getPageSource();
echo $content;
$driver->quit();
?>
上述代码经过测试,是可以正常访问的
到此为止。
发布者:彬彬笔记,转载请注明出处:https://www.binbinbiji.com/php/2856.html