I'm working with chrome-php/chrome right now. I have problems when to execute keyboard key press for Enter. Are there any ways to do that ?
I have already trying to execute the code but there are no options. Maybe there are alternatives how to do that ?
below is the code I use to test.
$browserFactory = new BrowserFactory();
// starts headless Chrome
$browser = $browserFactory->createBrowser([
'windowSize' => [1920, 1000],
]);
try {
$page = $browser->createPage();
$page->setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36');
$page->navigate('https://www.google.com/')->waitForNavigation();
try {
$page->mouse()->find('textarea')->click();
$page->keyboard()
->typeText('travel the world');
$page->keyboard()
->typeRawKey('Enter');
} catch (ElementNotFoundException $exception) {
// element not found
}
$pageTitle = $page->evaluate('document.title')->getReturnValue();
$page->screenshot()->saveToFile('bar.png');
} finally {
$browser->close();
}
As seen in this github issue - It should be possible to emulate ENTER by using the following snippet:
$params = [ "type" => "rawKeyDown", "windowsVirtualKeyCode" => 13, "unmodifiedText" => "\r", "text" => "\r" ]; $message = new Message('Input.dispatchKeyEvent', $params); $response = $this->page->getSession()->sendMessageSync($message, 5000); $params['type'] = 'char'; $message = new Message('Input.dispatchKeyEvent', $params); $response = $this->page->getSession()->sendMessageSync($message, 5000); $params['type'] = 'keyUp'; $message = new Message('Input.dispatchKeyEvent', $params); $response = $this->page->getSession()->sendMessageSync($message, 5000);