From b82bc5fbd48a7f82e16c3fd28d6db462a02a9a86 Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Mon, 3 Feb 2020 14:52:18 -0800 Subject: [PATCH] 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 --- src/dom.ts | 4 +++- test/queryselector.spec.js | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/dom.ts b/src/dom.ts index 1efbe3e855..e27572f5e9 100644 --- a/src/dom.ts +++ b/src/dom.ts @@ -511,7 +511,9 @@ function normalizeSelector(selector: string): string { const eqIndex = selector.indexOf('='); if (eqIndex !== -1 && selector.substring(0, eqIndex).trim().match(/^[a-zA-Z_0-9-]+$/)) return selector; - if (selector.startsWith('//')) + // If selector starts with '//' or '//' prefixed with multiple opening + // parenthesis, consider xpath. @see https://github.com/microsoft/playwright/issues/817 + if (/^\(*\/\//.test(selector)) return 'xpath=' + selector; if (selector.startsWith('"')) return 'text=' + selector; diff --git a/test/queryselector.spec.js b/test/queryselector.spec.js index 54c6e60712..38a3de150c 100644 --- a/test/queryselector.spec.js +++ b/test/queryselector.spec.js @@ -177,6 +177,11 @@ module.exports.describe = function({testRunner, expect, selectors, FFOX, CHROMIU const element = await page.$('//html/body/section'); expect(element).toBeTruthy(); }); + it('should auto-detect xpath selector with starting parenthesis', async({page, server}) => { + await page.setContent('
test
'); + const element = await page.$('(//section)[1]'); + expect(element).toBeTruthy(); + }); it('should auto-detect text selector', async({page, server}) => { await page.setContent('
test
'); const element = await page.$('"test"');