基于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 #指定端口监听进程
wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw==
[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.同上

三、在docker安装

1.创建docker-compose.yml

version: "3"

services:
  selenium:
    build:
      context: .
    environment:
      TZ: 'Asia/Shanghai'
    shm_size: 2gb
    ports:
      - "4444:4444"
    networks:
      - selenium
    restart: always
    logging:
      driver: "json-file"
      options:
        max-size: "10m"
        max-file: "3"

networks:
  selenium:
    driver: bridge

2.创建Dockerfile

FROM selenium/standalone-chrome:4.0.0-rc-1-prerelease-20210713

ENV LANGUAGE zh_CN.UTF-8
ENV LANG zh_CN.UTF-8

3.执行  docker-compose , 完成

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值