Skip to content

Commit b82bc5f

Browse files
authored
feat: treat selectors with leading '(//' as xpath (#821)
This starts treating the following selectors as xpath: - `page.$('//div')` - `page.$('(//div)[1]')` - `page.$('((((//div))))[1]')` (and generally, any number of leading openting parenthesis) Fixes #817
1 parent cea036a commit b82bc5f

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/dom.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,9 @@ function normalizeSelector(selector: string): string {
511511
const eqIndex = selector.indexOf('=');
512512
if (eqIndex !== -1 && selector.substring(0, eqIndex).trim().match(/^[a-zA-Z_0-9-]+$/))
513513
return selector;
514-
if (selector.startsWith('//'))
514+
// If selector starts with '//' or '//' prefixed with multiple opening
515+
// parenthesis, consider xpath. @see https://github.com/microsoft/playwright/issues/817
516+
if (/^\(*\/\//.test(selector))
515517
return 'xpath=' + selector;
516518
if (selector.startsWith('"'))
517519
return 'text=' + selector;

test/queryselector.spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ module.exports.describe = function({testRunner, expect, selectors, FFOX, CHROMIU
177177
const element = await page.$('//html/body/section');
178178
expect(element).toBeTruthy();
179179
});
180+
it('should auto-detect xpath selector with starting parenthesis', async({page, server}) => {
181+
await page.setContent('<section>test</section>');
182+
const element = await page.$('(//section)[1]');
183+
expect(element).toBeTruthy();
184+
});
180185
it('should auto-detect text selector', async({page, server}) => {
181186
await page.setContent('<section>test</section>');
182187
const element = await page.$('"test"');

0 commit comments

Comments
 (0)