akdr/selma

用于简化PHP-Webdriver使用的库

v0.6.0 2022-12-06 18:58 UTC

README

一个尝试简化网络爬取使用的PHP-Webdriver包装器。

使用方法

要使用此包装器,您需要有一个正在运行的Selenium Hub。

要设置它,在docker目录中谷歌搜索或使用docker-compose.yml。文件中包含有在安装了docker后如何启动的说明。

导航

导航处理浏览器导航和操作。它由Element类使用,在尝试爬取之前需要启动。

// Example of starting a navigation. The first argument is the location of Selenium Hub and 
// the second is Chrome-options.
use ComparicoAB\Selma\Navigation;
$nav = new Navigation('https://:4444/wd/hub', ["window-size=1920,4000", "--headless", "--disable-gpu", "--no-sandbox"]);
可用方法

Element

Element类处理所有DOM相关操作。它搜索DOM元素,提取文本,填充输入和点击元素。

// Example of using the Element to fill out a form and then clicking the submit button.
use Akdr\Selma\Element;
use Akdr\Selma\Navigation;

$nav = new Navigation('https://:4444/wd/hub', ["window-size=1920,4000", "--headless", "--disable-gpu", "--no-sandbox"]);

// First time we need to initiate the Element to use our browser, 
// later we can keep using it with the method Set.

// Enter the text "Selma is being used" into the input.
$element = new Element($nav, [
    'selector' => '#form-input',
    'input' => "Selma is being used"
]);

// Click the submit button
$element->set([
    'selector' => '#submit-button',
    'click' => true,
    'delay' => 400000
]);

//Select the response, which is a span without a class or id inside a container.

$container = $element->set([
    'selector' => '.container'
]);

$response = $element->set([
    'element' => $container->element,
    'selector' => 'span',
    'attribute' => 'text'
]);

// Finally, read the response and get the integer while removing the rest of the text.
$response->getValue('int');
可用方法

要获取Element并操作它,您可以使用构造函数或set()方法。它们都接受一个数组作为参数,并按照提供的顺序执行。必须设置selector键,并且始终(除'element'外)为第一个。

要从属性获取值,将 ->getValue('int'|'float'|null) 连接到构造函数或set()。

公共属性

如果您需要检索RemoteWebElement,请调用类中名为'element'的属性。

如果您需要检索类-bool,请调用属性'hasClass'。

示例

use ComparicoAB\Selma\Navigation;
use ComparicoAB\Selma\Element;

// Setup the browser and initiate the element class.
$nav = new Navigation('https://:4444/wd/hub', ["window-size=1920,4000", "--headless", "--disable-gpu", "--no-sandbox"]);
$element = new Element($nav, []);

// <a href="https://comparico.se class="title">Title Number 3.14</a>
$title = $element->set([
    'selector' => 'a',
    'hasClass' => 'title',
    'attribute' => 'href'
]);

// Returns the RemoteWebElement
$title->element; // Facebook\WebDriver\Remote\RemoteWebElement

// Returns the class-bool
$title->hasClass; // true

// Fetch the attribute
$title->getValue(); // Title Number 3.14
$title->getValue('int'); // 314
$title->getValue('float'); // 3.14