How to keep selenium alive and detect if session has been expired? #839
-
Q1: Is possible to keep selenium alive (long live)?Currently im using Q2: How to detect if session has been expired?Expected: Current condition: How to reproduce:
usage: <?php
namespace Facebook\WebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\WebDriverCapabilityType;
use Facebook\WebDriver\Remote\LocalFileDetector;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverExpectedCondition;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\WebDriverKeys;
use Facebook\WebDriver\Exception\ExpectedException;
use Facebook\WebDriver\Exception\WebDriverCurlException;
use Facebook\WebDriver\Exception\NoSuchElementException;
use Facebook\WebDriver\Exception\TimeOutException;
use Facebook\WebDriver\Exception\InvalidSessionIdException;
use Facebook\WebDriver\Exception\SessionNotCreatedException;
use Facebook\WebDriver\WebDriverWindow;
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
/*
USAGE : php test.php [keyword] [sessionID]
*/
$search = isset($argv[1])?$argv[1]:null;
$id = isset($argv[2])?$argv[2]:null;
if(!$search){
echo 'Keyword is required'.PHP_EOL;
exit;
}
require_once('vendor/autoload.php');
$options = new ChromeOptions();
$options->addArguments(array(
'no-sandbox',
'disable-infobars',
'disable-gpu',
//'headless',
));
$options->setExperimentalOption('excludeSwitches', ['enable-automation']);
$host = 'http://localhost:4444';
//$host = 'http://localhost:4444/wd/hub';
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY, $options);
//start here
if($id){
echo 'continue session '.PHP_EOL;
try{
$driver = RemoteWebDriver::createBySessionID($id,$host);
}catch(InvalidSessionIdException $e){
/*
This not triggered when sessionId not found/invalid
*/
echo 'continue session failed '.PHP_EOL;
$driver = RemoteWebDriver::create($host, $capabilities, 5000);
$driver->get('http://www.wikipedia.org/');
}catch(SessionNotCreatedException $e){echo 1;exit;
/*
This not triggered when sessionId not found/invalid
*/
echo 'continue session failed '.PHP_EOL;
$driver = RemoteWebDriver::create($host, $capabilities, 5000);
$driver->get('http://www.wikipedia.org/');
}
}else{
echo 'create session '.PHP_EOL;
$driver = RemoteWebDriver::create($host, $capabilities, 5000);
$driver->get('http://www.wikipedia.org/');
}
$driver->findElement(WebDriverBy::id('searchInput'))->sendKeys($search)->submit();
$id = $driver->getSessionID();
echo 'id: '.$id.PHP_EOL;
//no $driver->quit() because i will let this browser open for future purpose Browser: Chromedriver 86 |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 1 reply
-
Selenium will keep running until you kill it (ctrl-c, etc).
Generally speaking it's more typical to have a script create the session, perform all work, and then end the session - i.e. all in the same PHP Process. It is unusual to start a session in one process, and then have a separate process use the same session which is what you seem to be doing by echoing the ID and then passing it in as a CLI argument the next time you call the script. Yes, it is possible, but it's also a little bit dirty. |
Beta Was this translation helpful? Give feedback.
-
thanks for the reply @andrewnicols Q1: my bad. i just forgot it. thanks for remembering me Q2: check if selenium session still exists
i will make simple question for Q2: |
Beta Was this translation helpful? Give feedback.
-
is there anyone know how to catch error if sessionId is not valid? |
Beta Was this translation helpful? Give feedback.
-
Also intersted |
Beta Was this translation helpful? Give feedback.
-
this is how i check if a 'session is present'
|
Beta Was this translation helpful? Give feedback.
Selenium will keep running until you kill it (ctrl-c, etc).
The
timeout
parameter is unrelated and is the value used for waiting for a response between Selenium and the driver for each browser (i.e. Chromedriver).