'Using puppeteers page.select() function in puphpeteer

I am using "nesk/puphpeteer": "^2.0" and I want to select the following dropdown of a datatable:

image

Find below my minimum example:

<?php

require_once '../vendor/autoload.php';

use Nesk\Puphpeteer\Puppeteer;
use Nesk\Rialto\Data\JsFunction;

$debug = true;

$puppeteer = new Puppeteer([
    'read_timeout' => 100,
    'debug' => $debug,
]);
$browser = $puppeteer->launch([
    'headless' => !$debug,
    'ignoreHTTPSErrors' => true,
]);

$page = $browser->newPage();
$page->goto('https://www.psacard.com/auctionprices#0%7Cpokemon');

// select dropdown
// drop-down activate
$selectElemDropDown = $page->querySelectorXPath('//*[@id="DataTables_Table_0_length"]/label/select');
$selectElemDropDown[0]->click();
$selectElemOptTwo = $page->querySelectorXPath('//*[@id="DataTables_Table_0_length"]/label/select/option[2]');
$selectElemOptTwo[0]->click();

$browser->close();

I tried clicking on the dropdown and then clicking again to select the element, which does not work.

I puppeteer JS there is a function called page.select(selector, ...values).

How can I do this function in puphpteer in my minimum viable example?

I appreciate your replies!

php


Solution 1:[1]

page.select is also available in php, it accept a css selector instead of xpath.

<?php
require_once './vendor/autoload.php';

use Nesk\Puphpeteer\Puppeteer;
use Nesk\Rialto\Data\JsFunction;

$debug = true;

$puppeteer = new Puppeteer([
    'read_timeout' => 100,
    'debug' => $debug,
]);
$browser = $puppeteer->launch([
    'headless' => !$debug,
    'ignoreHTTPSErrors' => true,
]);

$page = $browser->newPage();
$page->goto('https://www.psacard.com/auctionprices#0%7Cpokemon');
// wait for select element to appear
$page->waitForSelector('select[name=DataTables_Table_0_length]');
// set select value to 100
$page->select('select[name=DataTables_Table_0_length]', '100');

$browser->close();

Solution 2:[2]

You must require autoload correctly if you use it this way and it doesn't work you are doing something wrong. Also enter terminal from project folder and use composer dump-autoload after you add the require 'vendor/autoload.php' line to project.

require 'vendor/autoload.php';
use Nesk\Puphpeteer\Puppeteer;
use Nesk\Rialto\Data\JsFunction;

$debug = true;

$puppeteer = new Puppeteer([
    'read_timeout' => 100,
    'debug' => $debug,
]);
$browser = $puppeteer->launch([
    'headless' => !$debug,
    'ignoreHTTPSErrors' => true,
]);

$page = $browser->newPage();
$page->goto('https://www.psacard.com/auctionprices#0%7Cpokemon');
$page->waitForTimeout(8000);
// set select value to 100
$page->select('select[name=DataTables_Table_0_length]', '100');

$browser->close();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 emptyhua
Solution 2 Cellphonemega LLC