comparicoab / selma
简化PHP-Webdriver使用的库
v0.6.0
2022-12-06 18:58 UTC
Requires
- php: >=8.0
- ext-json: *
- jbzoo/data: 5.*
- jbzoo/utils: 5.*
- php-webdriver/webdriver: >=1.13.0
Requires (Dev)
- phpunit/phpunit: ^9
This package is auto-updated.
Last update: 2024-09-06 23:21:13 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