Rayrun
← Back to Discord Forum

Why this difference in ```page.keyboard.press('Enter')``` depending on how the code is executed?

ironknight00posted in #help-playwright
Open in Discord
ironknight00
ironknight00

Hi!

I have noticed that on two different laptops there is a difference in the behavior of page.keyboard.press('Enter') depending on how the code is executed. If I execute the code using node index.js I get a different behaviour, I get a line break (the wrong one) compared to when I execute the code using the Code Runner extension on VS Code, which submits a form e.g. (the correct behaviour).

The setup that I did:

mkdir try_playwright
cd try_playwright
npm init -y

Added in the package.json: "type": "module" Then: npm install playwright

I go to VS Code, create a file index.js and try this bit of code:

import { chromium } from 'playwright';

const browser = await chromium.launch({ headless: false });

const page = await browser.newPage();

await page.goto('https://google.com/');

await page.click('button:has-text("I agree")');

await page.type('textarea[title="Search"]', 'hello world');

await page.keyboard.press('Enter');

await page.waitForTimeout(10000);
await browser.close();

So I have tried this on two configurations:

  • macOS Ventura 13.4.1, Apple M1. Node: v20.5.1. NPM: 9.8.0
  • Windows 11 64 bits (10.0, build 22621). Node: v18.17.0. NPM: 9.8.1

Recap of the behaviours:

  • Run with Code Runner extension on VS Code: does exactly what is expected.
  • Run with node index.js command on terminal: types "hello world" in the search bar and then goes to a new line.

Notes:

  • await page.click('button:has-text("I agree")'); is only useful is you're in EU. If your browser is in a different language, you should change the "I agree".
  • The "Search" title of the text area search bar in Google will vary depending on your browser's language too.

This thread is trying to answer question "Why does the behavior of ```page.keyboard.press('Enter')``` differ depending on how the code is executed?"

0 replies
TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.