I'm trying to get a Debian VM with selenium PHP running again. It was working a few months ago but I got pulled of the project.
I tried running selenium (in an ssh connection) as the nginx user and as root. Both produce the same error
java -jar ./selenium-server-4.1.1.jar standalone
Starting ChromeDriver 97.0.4692.71 (adefa7837d02a07a604c1e6eff0b3a09422ab88d-refs/branch-heads/4692@{#1247}) on port 53084
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
12:10:51.070 WARN [SeleniumSpanExporter$1.lambda$export$0] - {"traceId": "a82ccdad343550c9da81ad23dd3c99fb","eventTime": 1658506251052025402,"eventName": "exception","attributes": {"driver.url": "http:\u002f\u002flocalhost:53084","exception.message": "Error while creating session with the driver service. Stopping driver service: Could not start a new session. Response code 500. Message: unknown error: Chrome failed to start: exited abnormally.\n (unknown error: DevToolsActivePort file doesn't exist)\n (The process started from chrome location \u002fusr\u002fbin\u002fgoogle-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)\nBuild info: version: '4.1.1', revision: 'e8fcc2cecf'\nSystem info: host: 'selenium.questiondevelopment.com', ip: '43.35.51.12', os.name: 'Linux', os.arch: 'amd64', os.version: '4.19.0-18-amd64', java.version: '11.0.13'\nDriver info: driver.version: unknown","exception.stacktrace": "org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Response code 500. Message: unknown error: Chrome failed to start: exited abnormally.\n (unknown error: DevToolsActivePort file doesn't exist)\n (The process started from chrome location \u002fusr\u002fbin\u002fgoogle-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)\nBuild info: version: '4.1.1', revision: 'e8fcc2cecf'\nSystem info: host: 'selenium.questiondevelopment.com', ip: '43.35.51.12', os.name: 'Linux', os.arch: 'amd64', os.version: '4.19.0-18-amd64', java.version: '11.0.13'\nDriver info: driver.version: unknown\n\tat org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:126)\n\tat org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:84)\n\tat org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:62)\n\tat org.openqa.selenium.grid.node.config.DriverServiceSessionFactory.apply(DriverServiceSessionFactory.java:131)\n\tat org.openqa.selenium.grid.node.config.DriverServiceSessionFactory.apply(DriverServiceSessionFactory.java:65)\n\tat org.openqa.selenium.grid.node.local.SessionSlot.apply(SessionSlot.java:143)\n\tat org.openqa.selenium.grid.node.local.LocalNode.newSession(LocalNode.java:314)\n\tat org.openqa.selenium.grid.distributor.local.LocalDistributor.startSession(LocalDistributor.java:513)\n\tat org.openqa.selenium.grid.distributor.local.LocalDistributor.newSession(LocalDistributor.java:440)\n\tat org.openqa.selenium.grid.distributor.local.LocalDistributor$NewSessionRunnable.handleNewSessionRequest(LocalDistributor.java:648)\n\tat org.openqa.selenium.grid.distributor.local.LocalDistributor$NewSessionRunnable.lambda$run$1(LocalDistributor.java:612)\n\tat java.base\u002fjava.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)\n\tat java.base\u002fjava.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)\n\tat java.base\u002fjava.lang.Thread.run(Thread.java:829)\n","exception.type": "org.openqa.selenium.SessionNotCreatedException","logger": "org.openqa.selenium.grid.node.config.DriverServiceSessionFactory","session.capabilities": "{\"browserName\": \"chrome\",\"platformName\": \"ANY\",\"platform\": \"ANY\"}\n"}}
12:10:51.092 WARN [SeleniumSpanExporter$1.lambda$export$0] - {"traceId": "a82ccdad343550c9da81ad23dd3c99fb","eventTime": 1658506251091847351,"eventName": "HTTP request execution complete","attributes": {"http.flavor": 1,"http.handler_class": "org.openqa.selenium.grid.sessionqueue.local.LocalNewSessionQueue","http.host": "localhost:4444","http.method": "POST","http.request_content_length": "122","http.scheme": "HTTP","http.status_code": 500,"http.target": "\u002fsession"}}
My code (which was working) is as follows
require __DIR__ . '/../vendor/autoload.php';
putenv('WEBDRIVER_CHROME_DRIVER=/var/www/chromedriver');
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\WebDriverBy;
$serverUrl = 'http://localhost:4444/';
$driver = RemoteWebDriver::create($serverUrl, DesiredCapabilities::chrome());
$driver->get('https://en.wikipedia.org/wiki/Selenium_(software)');
// Find search element by its id, write 'PHP' inside and submit
$driver->findElement(WebDriverBy::name('search')) // find search input element
->sendKeys('PHP') // fill the search box
->submit(); // submit the whole form
// Find element of 'History' item in menu by its css selector
$historyButton = $driver->findElement(
WebDriverBy::cssSelector('#ca-history a')
);
// Read text of the element and print it to output
echo 'About to click to a button with text: ' . $historyButton->getText();
// Click the element to navigate to revision history page
$historyButton->click();
// Make sure to always call quit() at the end to terminate the browser session
$driver->quit();
I assume its something to do with how the services are running. I tried making chromedrive run in the background as well. I also tried connecting via
$serverUrl = 'http://localhost:4444/wd/hub/';
But that did not help. I am completely stuck, I would appreciate any help.
Edit : I tried updating chrome to 103, updating the corresponding chromedriver and running celenium 4.3.0 and that changed nothing at all. I tried all different combinations of running selenium from different users. I also moved chromedriver into /usr/bin and tried assinging it different permissions. I can't figure this out.
CodePudding user response:
This error message...
Build info: version: '4.1.1', revision: 'e8fcc2cecf'\nSystem info: host: 'selenium.questiondevelopment.com', ip: '43.35.51.12', os.name: 'Linux', os.arch: 'amd64', os.version: '4.19.0-18-amd64', java.version: '11.0.13'\nDriver info: driver.version: unknown
...implies that the ChromeDriver was unable to initiate/spawn a new Browsing Context i.e. google-chrome and presumably the ChromeDriver version can't be detected and is unknown
Solution
Ensure that:
- Selenium is upgraded to current released Version 4.3.0.
- ChromeDriver is updated to current ChromeDriver v103.0 level.
- Chrome Browser is updated to current chrome=103.0 (as per chromedriver=103.0.5060.53 release notes).
CodePudding user response:
It turned out I was not creatinging the web driver with the proper arguments. This code fixed my issue :
$chromeOptions = new ChromeOptions();
$chromeOptions->addArguments(['--headless']);
$chromeOptions->addArguments(['--no-sandbox']);
$chromeOptions->addArguments(['--disable-extensions']);
$chromeOptions->addArguments(['--disable-gpu']);
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY_W3C, $chromeOptions);
https://github.com/php-webdriver/php-webdriver/wiki/Chrome
How do you run Selenium headless in PHP?
PHP Fatal error: Uncaught Error: Class 'Facebook\WebDriver\ChromeOptions' not found