微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

基于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.同上

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐