我有如下代码:
page.click('div.button-table div:contains(Who) div.square-button:nth-child(1)')
当操纵up的人运行此代码时,它将引发错误:
简报
Failed to execute 'querySelector' on 'Document': 'div.button-table div:contains(Who) div.square-button:nth-child(1)' is not a valid selector.
完整
Error: Evaluation failed: DOMException: Failed to execute 'querySelector' on 'Document': 'div.button-table div:contains(Who) div.square-button:nth-child(1)' is not a valid selector.
at __puppeteer_evaluation_script__:1:33
at ExecutionContext.evaluateHandle (node_modules/puppeteer/lib/ExecutionContext.js:124:13)
at <anonymous>
-- ASYNC --
at ExecutionContext.<anonymous> (node_modules/puppeteer/lib/helper.js:144:27)
at ElementHandle.$ (node_modules/puppeteer/lib/ExecutionContext.js:529:50)
at ElementHandle.<anonymous> (node_modules/puppeteer/lib/helper.js:145:23)
at Frame.$ (node_modules/puppeteer/lib/FrameManager.js:456:34)
at <anonymous>
-- ASYNC --
at Frame.<anonymous> (node_modules/puppeteer/lib/helper.js:144:27)
at Frame.click (node_modules/puppeteer/lib/FrameManager.js:735:31)
at Frame.<anonymous> (node_modules/puppeteer/lib/helper.js:145:23)
at Page.click (node_modules/puppeteer/lib/Page.js:973:29)
at Proxy.fillInAudience (libs/page/brand_create_quote_page.js:67:21)
at Proxy.<anonymous> (libs/logger.js:38:27)
at Context.it (ui/brand/create_quote_spec.js:53:16)
at <anonymous>
但是此选择器可以在Devtools中成功运行-$('div.button-table div:contains(Who) div.square-button:nth-child(1)').text()
我还弄清楚了如果我删除div:contains(Who)
,木偶戏演员可以在没有恐慌的情况下运行它。
有什么想法吗?
谢谢!
答案 0 :(得分:2)
木偶内部使用document.querySelector。这样您就可以使用任何CSS Selector。这里的问题是contains不是CSS选择器,而是JQuery功能。
好处是,您可以使用Chrome开发人员工具测试选择器,然后使用document.querySelector
对其进行测试。