I'm trying to use Playwright with Javascript and struggling to get the page.locator (https://playwright.dev/docs/locators) function to work.
I've created the file testLocator.js that I run with node .\testLocator.js in the command terminal of visual studio code.
My code:
const { test, expect } = require('@playwright/test');
const playwright = require('playwright');
const delay = ms => new Promise(res => setTimeout(res, ms));
(async () => {
// Code execution happens within here
const browser = await playwright["chromium"].launch({
headless: false
});
//context
const context = await browser.newContext();
//page
const page = await context.newPage();
//naviagate to the page
await page.goto("https://stackoverflow.com/");
const locator = page.locator('text=About');
delay(5000);
})();
When I run the code I get the following error:
C:\Users\todd.baker\PlaywrightTest\my-playwright-intro\testLocator.js:22
const locator = page.locator('text=About');
^
TypeError: page.locator is not a function
at C:\Users\todd.baker\PlaywrightTest\my-playwright-intro\testLocator.js:22:24
at processTicksAndRejections (node:internal/process/task_queues:96:5)
Why is page.locator
not recognized as a function when the playwright website that I've attached here claims that it is?
Found a way to create a package.json file and it was built with these contents:
{
"name": "my-playwright-intro",
"version": "1.0.0",
"description": "",
"main": "testLocator.js",
"dependencies": {
"agent-base": "^4.3.0",
"brace-expansion": "^1.1.11",
"async-limiter": "^1.0.1",
"balanced-match": "^1.0.2",
"buffer-crc32": "^0.2.13",
"buffer-from": "^1.1.2",
"core-util-is": "^1.0.3",
"concat-map": "^0.0.1",
"concat-stream": "^1.6.2",
"fd-slicer": "^1.1.0",
"extract-zip": "^1.7.0",
"debug": "^4.3.4",
"es6-promisify": "^5.0.0",
"glob": "^7.2.3",
"fs.realpath": "^1.0.0",
"https-proxy-agent": "^3.0.1",
"inflight": "^1.0.6",
"inherits": "^2.0.4",
"isarray": "^1.0.0",
"mkdirp": "^0.5.6",
"mime": "^2.6.0",
"jpeg-js": "^0.3.7",
"minimatch": "^3.1.2",
"ms": "^2.1.2",
"once": "^1.4.0",
"path-is-absolute": "^1.0.1",
"pend": "^1.2.0",
"playwright": "^0.10.0",
"process-nextick-args": "^2.0.1",
"playwright-core": "^0.10.0",
"proxy-from-env": "^1.1.0",
"pngjs": "^3.4.0",
"readable-stream": "^2.3.7",
"safe-buffer": "^5.1.2",
"es6-promise": "^4.2.8",
"uuid": "^3.4.0",
"util-deprecate": "^1.0.2",
"minimist": "^1.2.6",
"rimraf": "^2.7.1",
"ws": "^6.2.2",
"yauzl": "^2.10.0",
"wrappy": "^1.0.2",
"string_decoder": "^1.1.1",
"progress": "^2.0.3",
"typedarray": "^0.0.6"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}