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思必得07 天前
[Web自动化] Selenium基础介绍
前端·python·selenium·自动化·web自动化
0思必得08 天前
[Web自动化] Selenium简单使用
前端·python·selenium·自动化·web自动化
0思必得014 天前
[Web自动化] Requests模块响应的内容
运维·前端·python·自动化·html·web自动化
0思必得016 天前
[Web自动化] Requests模块基本使用
运维·前端·python·自动化·html·web自动化
0思必得018 天前
[Web自动化] JavaScriptAJAX与Fetch API
运维·前端·javascript·python·自动化·html·web自动化
0思必得018 天前
[Web自动化] Web安全基础
运维·前端·javascript·python·自动化·html·web自动化
0思必得021 天前
[Web自动化] JS基础语法与数据类型
前端·javascript·自动化·html·web自动化
0思必得025 天前
[Web自动化] CSS布局与定位
前端·css·自动化·html·web自动化
0思必得01 个月前
[Web自动化] CSS基础概念和介绍
前端·css·python·自动化·html·web自动化
0思必得01 个月前
[Web自动化] HTML5常见新增标签
前端·python·自动化·html5·web自动化