chrome-php / chrome

Instrument headless chrome/chromium instances from PHP
MIT License
2.2k stars 269 forks source link

Trying to click <option> tag inside <select> throws "Could not compute box model" in getPosition #592

Open sanderdlm opened 6 months ago

sanderdlm commented 6 months ago

Trying to click some options inside a select:

$select = $tab->dom()->querySelector('.some-specific-select');

$options = $select->querySelectorAll('option');

foreach ($options as $option) {
    $content = $option->getText();

    if ($content === 'Choose an Option...') {
        continue;
    }

    $option->click();

this throws:

PHP Fatal error:  Uncaught HeadlessChromium\Exception\DomException: -32000 - Could not compute box model. in /Users/marc434451/Sites/personal/shooz/vendor/chrome-php/chrome/src/Dom/Node.php:214
Stack trace:
#0 /Users/marc434451/Sites/personal/shooz/vendor/chrome-php/chrome/src/Dom/Node.php(115): HeadlessChromium\Dom\Node->assertNotError(Object(HeadlessChromium\Communication\Response))
#1 /Users/marc434451/Sites/personal/shooz/vendor/chrome-php/chrome/src/Dom/Node.php(128): HeadlessChromium\Dom\Node->getPosition()
#2 /Users/marc434451/Sites/personal/shooz/vendor/chrome-php/chrome/src/Dom/Node.php(174): HeadlessChromium\Dom\Node->hasPosition()
#3 /Users/marc434451/Sites/personal/shooz/src/Parsers/BananafingersParser.php(70): HeadlessChromium\Dom\Node->click()
#4 /Users/marc434451/Sites/personal/shooz/index.php(31): Shooz\Parsers\BananafingersParser->getDetailPageInfo(Object(Shooz\Domain\Listing))
#5 {main}

Am I doing something wrong? I assume getPosition fails on the option elements because they're invisible? I tried to click and focus the select first, but that didn't seem to do much. Is this even supported?

enricodias commented 6 months ago

I'm not sure if we can get the exact position of the options in a select. As a workaround you could focus on it and use the keyboard to go through the options.

sanderdlm commented 6 months ago

I'll try that, thanks!