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思必得04 天前
[Web自动化] CSS基础概念和介绍
前端·css·python·自动化·html·web自动化
0思必得04 天前
[Web自动化] HTML5常见新增标签
前端·python·自动化·html5·web自动化
0思必得09 天前
[Web自动化] HTML列表标签
运维·python·自动化·html·web自动化
0思必得011 天前
[Web自动化] HTML元素及DOM元素
前端·python·自动化·html·web自动化
0思必得011 天前
[Web自动化] HTML元素的定位(Xpath)
前端·python·自动化·html·web自动化
0思必得014 天前
[Web自动化] 开发者工具应用(Application)面板
运维·前端·python·自动化·web自动化·开发者工具
0思必得016 天前
[Web自动化] 开发者工具性能(Performance)面板
运维·前端·自动化·web自动化·开发者工具
0思必得018 天前
[Web自动化] 开发者工具控制台(Console)面板
前端·javascript·python·自动化·web自动化·开发者工具
0思必得018 天前
[Web自动化] 开发者工具元素(Elements)面板
运维·前端·自动化·web自动化·开发者工具
0思必得024 天前
[Web自动化] HTTP/HTTPS协议
前端·python·http·自动化·网络基础·web自动化