最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Are there any ways to press the Enter keyboard key? - Stack Overflow

programmeradmin0浏览0评论

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('/')->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();
}

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/')->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();
}
Share Improve this question edited Jan 30 at 10:40 DarkBee 15.6k8 gold badges72 silver badges117 bronze badges asked Jan 30 at 10:36 Madxion CorpMadxion Corp 271 silver badge10 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 2

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);

source - alex-rsk

发布评论

评论列表(0)

  1. 暂无评论