Web自动化Demo-PHP+Selenium

1.新建工程

打开PhpStorm新建工程如下:

打开终端输入如下命令安装selenium:

bash 复制代码
composer require php-webdriver/webdriver

2.编写代码

php 复制代码
<?php
require 'vendor/autoload.php';

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

class BaiduTest
{
    public function runTest()
    {
        $serverUrl = 'http://localhost:4444';
        $driver = RemoteWebDriver::create($serverUrl, DesiredCapabilities::chrome());
        sleep(3);
        try {
            $driver->get('https://www.baidu.com');
            $searchBox = $driver->findElement(WebDriverBy::id('kw'));
            $searchBox->sendKeys('Hello');
            sleep(2);
            $searchButton = $driver->findElement(WebDriverBy::id("su"));
            $searchButton->click();
        } finally {
            $driver->quit();
        }
    }
}

$test = new BaiduTest();
$test->runTest();

运行之前先开启一个终端执行:

bash 复制代码
chromedriver --port=4444
相关推荐
0思必得02 天前
[Web自动化] Selenium处理iframe和frame
前端·爬虫·python·selenium·自动化·web自动化
0思必得02 天前
[Web自动化] Selenium处理文件上传和下载
前端·爬虫·python·selenium·自动化·web自动化
0思必得06 天前
[Web自动化] 爬虫合规指南:从法律红线到安全实践
前端·爬虫·自动化·web自动化
0思必得09 天前
[Web自动化] 数据抓取、解析与存储
运维·前端·爬虫·selenium·自动化·web自动化
0思必得09 天前
[Web自动化] 爬虫之网络请求
前端·爬虫·python·selenium·自动化·web自动化
0思必得010 天前
[Web自动化] 处理爬虫异常
运维·爬虫·python·selenium·自动化·web自动化
0思必得016 天前
[Web自动化] Selenium浏览器对象方法(操纵浏览器)
前端·python·selenium·自动化·web自动化
0思必得017 天前
[Web自动化] Selenium浏览器对象属性
前端·python·selenium·自动化·web自动化
0思必得01 个月前
[Web自动化] Selenium基础介绍
前端·python·selenium·自动化·web自动化
0思必得01 个月前
[Web自动化] Selenium简单使用
前端·python·selenium·自动化·web自动化