From d466c1526394d0f6b831081659ad4b475311dc25 Mon Sep 17 00:00:00 2001 From: Huan LI Date: Wed, 18 Oct 2017 17:27:30 +0800 Subject: [PATCH] puppeteer does not support XPath, its a Selector. use a patch (fix #636) --- src/puppet-web/bridge.ts | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/puppet-web/bridge.ts b/src/puppet-web/bridge.ts index 5d792be4..e82beeac 100644 --- a/src/puppet-web/bridge.ts +++ b/src/puppet-web/bridge.ts @@ -638,9 +638,35 @@ export class Bridge extends EventEmitter { public async clickSwitchAccount(): Promise { log.verbose('PuppetWebBridge', 'clickSwitchAccount()') - const SELECTOR = `//div[contains(@class,'association') and contains(@class,'show')]/a[@ng-click='qrcodeLogin()']` + // https://github.com/GoogleChrome/puppeteer/issues/537#issuecomment-334918553 + async function waitForXpath(page: Page, xpath: string) { + const resultsHandle = await (page as any).evaluateHandle(xpathInner => { + const results = [] as any + const query = document.evaluate(xpathInner, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); + for (let i = 0, length = query.snapshotLength; i < length; ++i) { + results.push(query.snapshotItem(i)); + } + return results; + }, xpath); + const properties = await resultsHandle.getProperties(); + const result = [] as any + const releasePromises = [] as any + for (const property of properties.values()) { + const element = property.asElement(); + if (element) + result.push(element); + else + releasePromises.push(property.dispose()); + } + await Promise.all(releasePromises); + return result; + } + + const XPATH_SELECTOR = `//div[contains(@class,'association') and contains(@class,'show')]/a[@ng-click='qrcodeLogin()']` + // const DOM_ try { - const button = await this.page.$(SELECTOR) + // const button = await this.page.$(XPATH_SELECTOR) + const [button] = await waitForXpath(this.page, XPATH_SELECTOR) await button.click() // const button = await this.driver.driver.findElement(By.xpath( // "//div[contains(@class,'association') and contains(@class,'show')]/a[@ng-click='qrcodeLogin()']")) -- GitLab