Benjamin-Loison / php-webdriver

PHP client for Selenium/WebDriver protocol. Previously facebook/php-webdriver
MIT License
0 stars 0 forks source link

`StaleElementReferenceException: stale element reference: stale element not found` sometimes while no issue in Python #2

Open Benjamin-Loison opened 11 months ago

Benjamin-Loison commented 11 months ago

Bug description

selenium.php:

<?php

include_once 'vendor/autoload.php';

use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverWait;
use Facebook\WebDriver\WebDriverExpectedCondition;

$serverUrl = 'http://localhost:4444';

$desiredCapabilities = DesiredCapabilities::chrome();

/*$chromeOptions = new ChromeOptions();
$chromeOptions->addArgument('-headless');
$chromeOptions->setPreference('-headless', 2);
$desiredCapabilities->setCapability(ChromeOptions::CAPABILITY, $chromeOptions);*/

$driver = RemoteWebDriver::create($serverUrl, $desiredCapabilities);
$wait = new WebDriverWait($driver, 20);

$driver->get('https://www.marinetraffic.com');

function get($by)
{
    global $wait;
    return $wait->until(WebDriverExpectedCondition::presenceOfElementLocated($by));
}

get(WebDriverBy::xpath('//button[@mode="primary"]'))->click();

$driver->quit();
?>
$ php selenium.php 
PHP Fatal error:  Uncaught Facebook\WebDriver\Exception\StaleElementReferenceException: stale element reference: stale element not found
  (Session info: chrome=120.0.6099.71)
  (Driver info: chromedriver=120.0.6099.71 (9729082fe6174c0a371fc66501f5efc5d69d3d2b-refs/branch-heads/6099_56@{#13}),platform=Linux 5.15.0-89-generic x86_64) in /home/benjamin/vendor/php-webdriver/webdriver/lib/Exception/WebDriverException.php:112
Stack trace:
#0 /home/benjamin/vendor/php-webdriver/webdriver/lib/Remote/HttpCommandExecutor.php(287): Facebook\WebDriver\Exception\WebDriverException::throwException()
#1 /home/benjamin/vendor/php-webdriver/webdriver/lib/Remote/RemoteWebDriver.php(507): Facebook\WebDriver\Remote\HttpCommandExecutor->execute()
#2 /home/benjamin/vendor/php-webdriver/webdriver/lib/Remote/RemoteExecuteMethod.php(38): Facebook\WebDriver\Remote\RemoteWebDriver->execute()
#3 /home/benjamin/vendor/php-webdriver/webdriver/lib/Remote/RemoteWebElement.php(78): Facebook\WebDriver\Remote\RemoteExecuteMethod->execute()
#4 /home/benjamin/selenium.php(31): Facebook\WebDriver\Remote\RemoteWebElement->click()
#5 {main}
  thrown in /home/benjamin/vendor/php-webdriver/webdriver/lib/Exception/WebDriverException.php on line 112
#!/usr/bin/python3

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

options = Options()
#options.add_argument('-headless')
options.set_preference('permissions.default.image', 2)
browser = webdriver.Firefox(options=options)
wait = WebDriverWait(browser, 20)

browser.get('https://www.marinetraffic.com')

def get(by, id_):
    return wait.until(EC.presence_of_element_located((by, id_)))

get(By.XPATH, '//button[@mode="primary"]').click()

browser.quit()

How could the issue be reproduced

Not read.

Expected behavior

No response

Php-webdriver version

Not read.

PHP version

Not read.

How do you start the browser driver or Selenium server

Not read.

Selenium server / Selenium Docker image version

No response

Browser driver (chromedriver/geckodriver...) version

No response

Browser name and version

No response

Operating system

No response

Additional context

No response

Benjamin-Loison commented 11 months ago

elementToBeClickable solves the problem. Maybe I have not run as many times the Python script as the PHP one. Should find a theoretical reason for this issue.