IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> PHP知识库 -> 基于selenium的爬虫自动化测试相关Php-webdriver 在window和linux上的安装与使用教程 -> 正文阅读

[PHP知识库]基于selenium的爬虫自动化测试相关Php-webdriver 在window和linux上的安装与使用教程

一、在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.同上

  PHP知识库 最新文章
Laravel 下实现 Google 2fa 验证
UUCTF WP
DASCTF10月 web
XAMPP任意命令执行提升权限漏洞(CVE-2020-
[GYCTF2020]Easyphp
iwebsec靶场 代码执行关卡通关笔记
多个线程同步执行,多个线程依次执行,多个
php 没事记录下常用方法 (TP5.1)
php之jwt
2021-09-18
上一篇文章      下一篇文章      查看所有文章
加:2021-07-27 15:59:34  更:2021-07-27 16:00:35 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/3 0:59:37-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码